Hollow Concentric Octagon of Stars

This program prints a hollow concentric octagon using stars.

Problem Statement

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

Source Code

1 public class HollowConcentricOctagonStars {
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("* ");
8 else System.out.print(" ");
9 }
10 System.out.println();
11 }
12 }
13 }

Program Output

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

Explanation

The outer square border and both diagonals are marked with stars, so the combination of border and diagonal conditions approximates an octagon-like shape with cut corners.

Quick Links to Explore