Advanced Hybrid Hourglass Mosaic (Numbers + Alphabets)

This program prints an hourglass mosaic alternating numbers and alphabets.

Problem Statement

Write a Java program that prints an hourglass of 7 rows alternating numbers and alphabets cyclically.

Source Code

1 public class AdvancedHourglassMosaicNumbersAlphabets {
2 public static void main(String[] args) {
3 int n=7,num=1; char ch='A'; boolean number=true;
4 for(int i=0;i<n;i++){
5 for(int j=0;j<n;j++){
6 if(i<=j && i+j>=n-1){
7 if(number) System.out.print(num+++" ");
8 else {
9 System.out.print(ch+" ");
10 ch++;
11 if(ch>'Z') ch='A';
12 }
13 number=!number;
14 } else System.out.print(" ");
15 }
16 System.out.println();
17 }
18 }
19 }

Program Output

            1 A 2 
        B 3 C 4 D   
    5 E 6 F 7 G     
  H 8 I 9 J         
    10 K 11 L       
        M 12 N      
            13 O    

Explanation

Within the hourglass-shaped region, a boolean toggle alternates each printed cell between a sequential number and a sequential alphabet, so the hourglass is filled with a strictly alternating hybrid sequence.

Quick Links to Explore