Hollow Concentric Pentagon of Stars

This program prints a hollow concentric pentagon using stars.

Problem Statement

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

Source Code

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

Program Output

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

Explanation

The inner loop width is 2*i+1 rather than 2*i-1, widening the shape slightly, and stars are printed only at boundaries or the full base, approximating a pentagon-like hollow outline.

Quick Links to Explore