Hollow Concentric Hexagram with Numbers

This program prints a hollow concentric hexagram using numbers.

Problem Statement

Write a Java program that prints concentric hollow hexagrams with numbers up to 7 rows.

Source Code

1 public class HollowConcentricHexagramNumbers {
2 public static void main(String[] args) {
3 int n=7;
4 for(int i=0;i<n;i++){
5 for(int j=0;j<n;j++){
6 if(i==0 || i==n-1 || j==0 || j==n-1 || i==j || i+j==n-1){
7 System.out.print((i+1)+" ");
8 } else {
9 System.out.print(" ");
10 }
11 }
12 System.out.println();
13 }
14 }
15 }

Program Output

1 1 1 1 1 1 1 
2 2       2 2 
3   3   3   3 
4     4     4 
5   5   5   5 
6 6       6 6 
7 7 7 7 7 7 7 

Explanation

The outer loop iterates through rows from 0 to 6, giving 7 rows in total, and the inner loop iterates through columns from 0 to 6, ensuring a square grid. The condition checks for boundary positions (i==0, i==n-1, j==0, j==n-1) and diagonals (i==j, i+j==n-1). At these positions, the program prints the row number (i+1), which creates the hexagram outline, while non-boundary positions receive spaces to maintain the hollow structure. Using row numbers instead of stars makes each row distinct and emphasizes the concentric layering, demonstrating how nested loops combined with conditional checks can generate complex polygonal shapes with numeric labeling.

Quick Links to Explore