Hollow Concentric Octagon with Numbers

This program prints a hollow concentric octagon using numbers.

Problem Statement

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

Source Code

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

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 same border-and-diagonal octagon condition is used, but each marked cell prints the current row index instead of a star, so the octagon outline is labeled with numbers by row.

Quick Links to Explore