Star-Numeric-Alphabet Lattice Diamond
This program prints a diamond lattice alternating stars, numbers, and alphabets.
Problem Statement
Write a Java program that prints a diamond of 5 rows where each cell alternates stars, numbers, and alphabets cyclically.
Source Code
| 1 | public class LatticeDiamond { |
| 2 | public static void main(String[] args) { |
| 3 | int n=5, num=1; |
| 4 | char ch='A'; |
| 5 | for(int i=1; i<=n; i++) { |
| 6 | for(int j=i; j<n; j++) System.out.print(" "); |
| 7 | for(int j=1; j<=2*i-1; j++) { |
| 8 | int mod=j%3; |
| 9 | if(mod==1) System.out.print("* "); |
| 10 | else if(mod==2) System.out.print(num++ + " "); |
| 11 | else { |
| 12 | System.out.print(ch + " "); |
| 13 | ch++; |
| 14 | if(ch>'Z') ch='A'; |
| 15 | } |
| 16 | } |
| 17 | System.out.println(); |
| 18 | } |
| 19 | for(int i=n-1; i>=1; i--) { |
| 20 | for(int j=i; j<n; j++) System.out.print(" "); |
| 21 | for(int j=1; j<=2*i-1; j++) { |
| 22 | int mod=j%3; |
| 23 | if(mod==1) System.out.print("* "); |
| 24 | else if(mod==2) System.out.print(num++ + " "); |
| 25 | else { |
| 26 | System.out.print(ch + " "); |
| 27 | ch++; |
| 28 | if(ch>'Z') ch='A'; |
| 29 | } |
| 30 | } |
| 31 | System.out.println(); |
| 32 | } |
| 33 | } |
| 34 | } |
Program Output
* 1 A
* 2 B *
3 C * 4 D E
* 5 F * 6 G H *
7 I * 8 J K L M N
* 9 O * 10 P Q *
11 R * 12 S T U
* 13 V *
* 14 W
Explanation
The diamond shape built from increasing then decreasing rows is combined with a j%3 within-row cycle that alternates star, number, and alphabet, so a diamond mosaic of three symbol types is formed.