Advanced Hybrid Grid (Numbers, Alphabets, Stars)

This program prints a grid alternating numbers, alphabets, and stars in sequence.

Problem Statement

Write a Java program that prints a 6x6 grid alternating numbers, alphabets, and stars cyclically.

Source Code

1 public class AdvancedHybridGrid {
2 public static void main(String[] args) {
3 int n = 6, num=1;
4 char ch='A';
5 for(int i=0; i<n; i++) {
6 for(int j=0; j<n; j++) {
7 int mod=(i*n+j)%3;
8 if(mod==0) System.out.print(num++ + " ");
9 else if(mod==1) {
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 }
18 }

Program Output

1 A * 4 B * 
7 C * 10 D * 
13 E * 16 F * 
19 G * 22 H * 
25 I * 28 J * 
31 K * 34 L * 

Explanation

A running index (i*n+j)%3 is computed for every cell in row-major order, cycling through printing a number, an alphabet, or a star, producing a strictly repeating three-symbol sequence across the whole grid.

Quick Links to Explore