Numeric-Alphabet Star Mosaic

This program prints a mosaic alternating numbers, alphabets, and stars.

Problem Statement

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

Source Code

1 public class NumericAlphabetStarMosaic {
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 if((i+j)%3==0) System.out.print(num++ + " ");
8 else if((i+j)%3==1) {
9 System.out.print(ch + " ");
10 ch++;
11 if(ch>'Z') ch='A';
12 } else System.out.print("* ");
13 }
14 System.out.println();
15 }
16 }
17 }

Program Output

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

Explanation

The value (i+j)%3 cycles through three states, printing a number, an alphabet, or a star respectively, so the grid cycles through three different symbol types in a repeating diagonal-like mosaic.

Quick Links to Explore