Write a Java program to print diamond alphabets pattern using for loop.
package Alphabets; import java.util.Scanner; public class DiamondAlps1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Diamond Pattern of Alphabets Rows = "); int rows = sc.nextInt(); System.out.println("Printing Diamond Alphabets Pattern\n"); int i, j, k, alphabet = 64; for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (k = 1 ; k <= i * 2 - 1; k++ ) { System.out.print((char)(alphabet + k) ); } System.out.println(); } for (i = rows - 1 ; i > 0; i-- ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (k = 1 ; k <= i * 2 - 1; k++ ) { System.out.print((char)(alphabet + k) ); } System.out.println(); } } }
It is another way of writing the Java program to display the diamond pattern of alphabets.
package Alphabets; import java.util.Scanner; public class DiamondAlps2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Diamond Pattern of Alphabets Rows = "); int rows = sc.nextInt(); System.out.println("Printing Diamond Alphabets Pattern\n"); int i, j, k, alphabet = 65; for (i = 0; i <= rows - 1; i++ ) { for (j = rows - 1 ; j >= i; j-- ) { System.out.print(" "); } for (k = 0; k <= i; k++ ) { System.out.print((char)(alphabet + k) + " "); } System.out.println(); } for (i = 0; i <= rows - 1; i++) { for (j = -1 ; j <= i; j++ ) { System.out.print(" "); } for (k = 0; k <= rows - i - 2; k++ ) { System.out.print((char)(alphabet + k) + " "); } System.out.println(); } } }
Enter Diamond Pattern of Alphabets Rows = 10
Printing Diamond Alphabets Pattern
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 F G
A B C D E F G H
A B C D E F G H I
A B C D E F G H I J
A B C D E F G H I
A B C D E F G H
A B C D E F G
A B C D E F
A B C D E
A B C D
A B C
A B
A
This Java example is the other way to print the Diamond pattern of alphabets.
package Alphabets; import java.util.Scanner; public class DiamondAlps3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Diamond Pattern of Alphabets Rows = "); int rows = sc.nextInt(); System.out.println("Printing Diamond Alphabets Pattern\n"); int i, j, k, l, alphabet = 64; for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (k = i ; k >= 1; k-- ) { System.out.print((char)(alphabet + k)); } for (l = 2 ; l <= i; l++) { System.out.print((char)(alphabet + l)); } System.out.println(); } for (i = rows - 1 ; i > 0; i-- ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (k = i ; k >= 1; k-- ) { System.out.print((char)(alphabet + k)); } for (l = 2 ; l <= i; l++) { System.out.print((char)(alphabet + l)); } System.out.println(); } } }
Enter Diamond Pattern of Alphabets Rows = 15
Printing Diamond Alphabets Pattern
A
BAB
CBABC
DCBABCD
EDCBABCDE
FEDCBABCDEF
GFEDCBABCDEFG
HGFEDCBABCDEFGH
IHGFEDCBABCDEFGHI
JIHGFEDCBABCDEFGHIJ
KJIHGFEDCBABCDEFGHIJK
LKJIHGFEDCBABCDEFGHIJKL
MLKJIHGFEDCBABCDEFGHIJKLM
NMLKJIHGFEDCBABCDEFGHIJKLMN
ONMLKJIHGFEDCBABCDEFGHIJKLMNO
NMLKJIHGFEDCBABCDEFGHIJKLMN
MLKJIHGFEDCBABCDEFGHIJKLM
LKJIHGFEDCBABCDEFGHIJKL
KJIHGFEDCBABCDEFGHIJK
JIHGFEDCBABCDEFGHIJ
IHGFEDCBABCDEFGHI
HGFEDCBABCDEFGH
GFEDCBABCDEFG
FEDCBABCDEF
EDCBABCDE
DCBABCD
CBABC
BAB
A