pattern programs Set2
pattern programs Set2
Pattern 1 Pattern 2
ABCDE B
ABCD LL
ABC UUU
AB EEEE
Answer
import java.util.Scanner;
switch (choice) {
case 1:
System.out.println();
break;
case 2:
System.out.print(word.charAt(i));
System.out.println();
break;
default:
System.out.println("Incorrect choice");
break;
}
2) Write a program to generate the following output.
*#
*#*
*#*#
*#*#*
Answer
if (j % 2 == 0)
System.out.print("# ");
else
System.out.print("* ");
System.out.println();
} }}
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Answer
int a = 1;
System.out.print(a++ + "\t");
System.out.println();
1****
22***
333**
4444*
55555
Answer
System.out.print(i);
System.out.print('*');
System.out.println();
}
}
****5
***4
**3
*2
Answer
char ch = '*';
System.out.print(i);
System.out.println();
54321
4321
321
21
Answer
System.out.println();
JA
JAV
JAVA
Answer
System.out.println();
21
321
4321
54321
} System.out.println();
} }}
99999
77777
55555
33333
11111
14) 9
79
579
3579
13579
public class Pattern14
{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = i; j <= 9; j += 2) {
System.out.print(j + " ");
}
System.out.println( );
}}}
15)
9
97
975
9753
97531
public class Pattern
{
public static void main(String args[]) {
for (int i = 9; i >= 1; i -= 2) {
for (int j = 9; j >= i; j -= 2) {
System.out.print(j + " ");
}
System.out.println();
}}}
16)Wap to print the following pattern
1
23
456
7 8 9 10
11 12 13 14 15
public class Pattern
{
public static void main(String args[]) {
int k = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++)
{
System.out.print(k++ + " ");
}
System.out.println();
}}}
}
}
20)Write a program in Java to display the following patterns.
IC
ICS
ICSE
System.out.println(); } }}
Question 21
Write a program to generate a triangle or an inverted triangle till n
terms based upon the user's choice of triangle to be displayed.
Example 1
Input:
Type 1 for a triangle and type 2 for an inverted triangle
1
Enter the number of terms
5
Output:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Example 2
Input:
Type 1 for a triangle and type 2 for an inverted triangle
2
Enter the number of terms
6
Output:
666666
55555
4444
333
22
import java.util.Scanner;
int ch = in.nextInt();
int n = in.nextInt();
switch (ch) {
case 1:
for (int i = 1; i <= n; i++) {
System.out.println();
break;
case 2:
System.out.println();
break;
default:
System.out.println("Incorrect Choice");