Hollow Concentric Hexagon of Stars

This program prints a hollow concentric hexagon using stars.

Problem Statement

Write a Java program that prints a hollow concentric hexagon with 7 rows.

Source Code

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

Program Output

      * * * * * * * 
     *           * 
    *           * 
   * * * * * * * 
  *           * 
 *           * 
* * * * * * * 

Explanation

An extra middle-row condition (i==n/2+1) is added to the hollow hexagon boundaries, printing an additional full row of stars in the middle, giving the hexagon a concentric divided appearance.

Quick Links to Explore