Use nested for loops that displays a pattern.
public static void main(String[] strings) { System.out.println("\n\nPattern A"); for (int i = 1; i <= 6; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } System.out.println("\n\nPattern B"); for (int i = 1; i <= 6; i++) { for (int j = 1; j <= 7 - i; j++) { System.out.print(j + " "); } System.out.println(); } System.out.println("\n\nPattern C"); for (int i = 1; i <= 6; i++) { for (int k = 1; k <= 6 - i; k++) { System.out.print(" "); } for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } System.out.println("\n\nPattern D"); for (int i = 1; i <= 6; i++) { for (int k = i; k > 1; k--) { System.out.print(" "); } for (int j = 1; j <= 7 - i; j++) { System.out.print(j + " "); } System.out.println(); } }
Pattern A 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern B 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Pattern C 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern D 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1