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.
The patterns include the sideways triangle or the pyramid with different alphabet patterns, also the half hourglass patterns containing different patterns.
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 | 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(); for (i = 0; i <= rows; i++) { int ch = 65; for (j = 0; j <= i; j++) System.out.print((char)(ch + j) + " "); System.out.println(); } for (i = rows; i >= 0; i--) { int ch = 65; for (j = 0; j <= i - 1; j++) System.out.print((char)(ch + j) + " "); System.out.println(); } } } |
Output:
Enter the no. of rows: 5
A
A B
A B C
A B C D
A B C D E
A B C D E F
A B C D E
A B C D
A B C
A B
A
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 33 | import java.util.Scanner; public class Main { public static void main(String args[]) { int rows, i, j; int character = 65; int count = 1; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); rows = sc.nextInt(); for (i = 1; i <= rows / 2 + 1; i++) { for (j = 1; j <= i; j++) System.out.print((char)(character + (count *j) - 1) + " "); System.out.println(); count++; } for (i = 1; i <= rows / 2; i++) { for (j = 1; j <= rows / 2 + 1 - i; j++) System.out.print((char)(character + (count *j) - 1) + " "); System.out.println(); count++; } } } |
Output:
Enter the no. of rows: 9
A
B D
C F I
D H L P
E J O T Y
F L R X
G N U
H P
I