Nested loop pattern
Nested loop pattern
54321
4321
321
21
1
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 5; i >= 1; i--) {
for (int j = i; j >= 1; j--)
System.out.print( j + " ");
System.out.println();
}
}
}
1
21
321
4321
54321
5
44
333
2222
11111
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 5, j = 1; i >= 1; i--, j++) {
for (int k = 1; k <= j; k++)
System.out.print(i + " ");
System.out.println();
}
}
}
3
44
555
6666
77777
public class KboatPattern
{
public static void main(String args[]) {
for (int i = 3; i <= 7; i++) {
for (int j = 3; j <= i; j++)
System.out.print(i);
System.out.println();
}
}
}
Write a program to print the series given below.
5 55 555 5555 55555 555555
Answer
public class Pattern
{
public static void main(String args[]) {
ABCDE
ABCD
ABC
AB
A
Answer
54321
4321
321
21
1
Answer
}
}
Write a program in Java to display the following patterns.
J
JA
JAV
JAVA
Answer
1
22
333
4444
55555
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
1
Answer
import java.util.Scanner;
switch (ch) {
case 1:
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
case 2:
for (int i = n; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}