Advanced Hybrid Concentric Grid (Stars + Numbers)
This program prints a concentric grid alternating stars and numbers.
Problem Statement
Write a Java program that prints a 7x7 concentric grid alternating stars and numbers.
Source Code
| 1 | public class AdvancedHybridConcentricGrid { |
| 2 | public static void main(String[] args) { |
| 3 | int n=7,num=1; |
| 4 | for(int i=0;i<n;i++){ |
| 5 | for(int j=0;j<n;j++){ |
| 6 | int layer=Math.min(Math.min(i,n-1-i),Math.min(j,n-1-j)); |
| 7 | if(layer%2==0) System.out.print("* "); |
| 8 | else System.out.print(num+++" "); |
| 9 | } |
| 10 | System.out.println(); |
| 11 | } |
| 12 | } |
| 13 | } |
Program Output
* * * * * * * * 1 2 3 4 5 * * 6 * * * 7 * * 8 * 9 * 10 * * 11 * * * 12 * * 13 14 15 16 17 * * * * * * * *
Explanation
The layer value measures distance from the nearest edge for each cell, and even layers are filled with stars while odd layers are filled with sequentially incrementing numbers, producing alternating concentric rings.