In this tutorial, we will go through two different alphabet pattern programs in java programming. Before that, you may go through the following topic in java.
2 different Alphabet pattern program in Java
Both of the programs below take the number of rows as the user input and using for loop it prints the desired output.
Pattern 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.Scanner; public class Main { public static void main(String args[]) { int rows, i, j; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); rows = sc.nextInt(); //upper part for (i = rows; i >= 0; i--) { int character = 65; for (j = 0; j <= i; j++) System.out.print((char)(character + j) + " "); System.out.println(); } //lower part for (i = 0; i <= rows; i++) { int character = 65; for (j = 0; j <= i; j++) System.out.print((char)(character + j) + " "); System.out.println(); } } } |
Output:
Enter the no. of rows: 5
A B C D E F
A B C D E
A B C D
A B C
A B
A
A
A B
A B C
A B C D
A B C D E
A B C D E F
Pattern 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.Scanner; public class Main { public static void main(String args[]) { int rows, i, j; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); rows = sc.nextInt(); //upper part for (i = rows; i >= 0; i--) { int character = 65; for (j = i; j >= 0; j--) System.out.print((char)(character + j) + " "); System.out.println(); } //lower part for (i = 0; i <= rows; i++) { int character = 65; for (j = i; j >= 0; j--) System.out.print((char)(character + j) + " "); System.out.println(); } } } |
Output:
Enter the no. of rows: 5
F E D C B A
E D C B A
D C B A
C B A
B A
A
A
B A
C B A
D C B A
E D C B A
F E D C B A