Numeric-Alphabet Star Grid
This program prints a grid alternating numbers, alphabets, and stars.
Problem Statement
Write a Java program that prints a 5x5 grid alternating numbers, alphabets, and stars.
Source Code
| 1 | public class NumericAlphabetStarGrid { |
| 2 | public static void main(String[] args) { |
| 3 | int n = 5, 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 E * 13 F * * 19 G * 22 I * 25 J *
Explanation
The same three-way cyclic condition (i+j)%3 as before is applied on a smaller 5x5 grid, cycling through numbers, alphabets, and stars to build a smaller hybrid mosaic grid.