Hollow Concentric Pentagon with Numbers

This program prints hollow concentric pentagons using numbers.

Problem Statement

Write a Java program that prints concentric hollow pentagons with numbers up to 5 rows.

Source Code

1 public class HollowConcentricPentagonNumbers {
2 public static void main(String[] args) {
3 int n=5;
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<=2*i+1; j++) {
7 if(j==1 || j==2*i+1 || i==n) System.out.print(i);
8 else System.out.print(" ");
9 }
10 System.out.println();
11 }
12 }
13 }

Program Output

    1    
   2 2   
  3   3  
 4     4 
555555555

Explanation

The same widened hollow pentagon boundary logic is used, but the printed symbol is the row index rather than a star, so each row's boundary is labeled with its own number.

Quick Links to Explore