Star Hourglass Pattern

This program prints an hourglass pattern of stars.

Problem Statement

Write a Java program that prints an hourglass pattern of stars with 5 rows.

Source Code

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

Program Output

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

Explanation

The first loop builds an inverted pyramid where stars decrease row by row, and the second builds an upright pyramid where stars increase, together forming a symmetric hourglass shape.

Quick Links to Explore