Numeric-Alphabet Star Mosaic Diamond
This program prints a diamond mosaic alternating numbers, alphabets, and stars.
Problem Statement
Write a Java program that prints a diamond mosaic alternating numbers, alphabets, and stars with 5 rows.
Source Code
| 1 | public class MosaicDiamond { |
| 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 | if(j%3==1) System.out.print(num++ + " "); |
| 9 | else if(j%3==2) { |
| 10 | System.out.print(ch + " "); |
| 11 | ch++; |
| 12 | if(ch>'Z') ch='A'; |
| 13 | } else System.out.print("* "); |
| 14 | } |
| 15 | System.out.println(); |
| 16 | } |
| 17 | for(int i=n-1; i>=1; i--) { |
| 18 | for(int j=i; j<n; j++) System.out.print(" "); |
| 19 | for(int j=1; j<=2*i-1; j++) { |
| 20 | if(j%3==1) System.out.print(num++ + " "); |
| 21 | else if(j%3==2) { |
| 22 | System.out.print(ch + " "); |
| 23 | ch++; |
| 24 | if(ch>'Z') ch='A'; |
| 25 | } else System.out.print("* "); |
| 26 | } |
| 27 | System.out.println(); |
| 28 | } |
| 29 | } |
| 30 | } |
Program Output
1 A *
2 B * 3
4 C * 5 D *
6 E * 7 F * 8 G *
9 H * 10 I * 11 J * 12 K * 13 L *
14 M * 15 N * 16 O *
17 P * 18 Q *
19 R * 20 S
21 T *
Explanation
The diamond structure of increasing then decreasing rows is combined with a j%3 cyclic condition, so each position within a row alternates between a number, an alphabet, and a star, forming a mosaic diamond.