Advanced Hybrid Hourglass Mosaic
This program prints an hourglass mosaic alternating stars, numbers, and alphabets.
Problem Statement
Write a Java program that prints an hourglass of 7 rows alternating stars, numbers, and alphabets cyclically.
Source Code
| 1 | public class AdvancedHybridHourglassMosaic { |
| 2 | public static void main(String[] args) { |
| 3 | int n=7,num=1; |
| 4 | char ch='A'; |
| 5 | for(int i=0;i<n;i++){ |
| 6 | for(int j=0;j<n;j++){ |
| 7 | if(i<=j && i+j>=n-1){ |
| 8 | int mod=(i+j)%3; |
| 9 | if(mod==0) System.out.print("* "); |
| 10 | else if(mod==1) System.out.print(num+++" "); |
| 11 | else { |
| 12 | System.out.print(ch+" "); |
| 13 | ch++; |
| 14 | if(ch>'Z') ch='A'; |
| 15 | } |
| 16 | } else System.out.print(" "); |
| 17 | } |
| 18 | System.out.println(); |
| 19 | } |
| 20 | } |
| 21 | } |
Program Output
* 1 A *
2 B * 3 C
* 4 D * 5 E *
6 F * 7 G * 8 H
* 9 I * 10 J *
11 K * 12 L
* 13 M *
Explanation
The hourglass region condition (i<=j && i+j>=n-1) selects only relevant cells, and within that region the (i+j)%3 cycle alternates star, number, and alphabet, creating a hybrid mosaic hourglass.