In this tutorial, we will go through different alphabet patterns in java. Before that, you may go through the following topic in java.
Both of the programs below take the user input for the number of rows to be printed for the alphabet pattern in java for the diamond shape.
Alphabet diamond pattern in java
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 34 35 36 37 38 39 40 41 42 | import java.util.Scanner; public class Main { public static void main(String args[]) { int rows, i, j, k; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); rows = sc.nextInt(); System.out.print("Diamond Shape Pattern: \n\n"); for (i = 0; i <= rows; i++) { int ch = 65; for (j = rows; j >= i; j--) { System.out.print(" "); } for (k = 0; k <= i; k++) { System.out.print((char)(ch + k) + " "); } System.out.println(); } for (i = 0; i <= rows; i++) { int ch = 65; for (j = -1; j <= i; j++) { System.out.print(" "); } for (k = 0; k <= (rows - 1) - i; k++) { System.out.print((char)(ch + k) + " "); } System.out.println(); } } } |
Output:
Hollow Diamond Alphabet Pattern in java
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import java.util.Scanner; public class Main { public static void main(String args[]) { int rows, i, j; int ch = 65; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); rows = sc.nextInt(); System.out.print("Diamond Shape Pattern: \n\n"); //Upper part of diamond for (i = 1; i <= rows; i++) { for (j = rows; j > i; j--) System.out.print(" "); System.out.print((char) ch++); for (j = 1; j < (i - 1) *2; j++) System.out.print(" "); if (i == 1) System.out.print("\n"); else System.out.print((char) ch++ + "\n"); } //lower part of diamond ch = 65; for (i = rows - 1; i >= 1; i--) { for (j = rows; j > i; j--) System.out.print(" "); System.out.print((char) ch++); for (j = 1; j < (i - 1) *2; j++) System.out.print(" "); if (i == 1) System.out.print("\n"); else System.out.print((char) ch++ + "\n"); } } } |
Output: