Print Sandglass Star Pattern in Java

Topic: Print Sandglass Star Pattern in Java

The sandglass star pattern resembles an hourglass or sand timer. It consists of a decreasing number of stars printed in a centered format followed by an increasing number of stars, also centered. This creates a symmetric shape around the middle line.

In Java, this pattern helps beginners understand nested loops, space and star printing control, and how to mirror patterns.

Examples

Input: 5

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

Input: 3

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

Edge Case (Input = 1):

*

Interviewer Expectations

The interviewer is checking your ability to:

  • Understand nested loop structures
  • Control spaces and star counts symmetrically
  • Write clean, readable code with proper formatting
  • Break down the problem into two mirrored parts

Approach

We split the sandglass into two halves:

  • Upper half: Print decreasing stars (2*n - 1, 2*n - 3, ..., 1) with increasing leading spaces.
  • Lower half: Print increasing stars (3, 5, ..., 2*n - 1) with decreasing leading spaces.

Dry Run (n = 3)

Upper half:

  • Row 0: 0 spaces, 5 stars → *****
  • Row 1: 1 space, 3 stars → ***
  • Row 2: 2 spaces, 1 star → *

Lower half:

  • Row 1: 1 space, 3 stars → ***
  • Row 2: 0 spaces, 5 stars → *****

Java Program

public class SandglassPattern {
    public static void main(String[] args) {
        int n = 5;

        // Upper half
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * (n - i) - 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // Lower half
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                System.out.print(" ");
            }
            for (int j = 0; j < 2 * i + 1; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********

Possible Followup Questions with Answers

1. How to make the pattern hollow?

Use conditions to print "*" only at the first and last star positions per line, and print spaces in between.

if (j == 0 || j == totalStars - 1)
    System.out.print("*");
else
    System.out.print(" ");

2. How to take the height from user input?

Scanner sc = new Scanner(System.in);
int n = sc.nextInt();

3. What happens if we start both loops from 0?

You will duplicate the middle line. We use i = 1 in the lower half to avoid repeating the center of the hourglass.

4. Can this be done with a single loop?

Yes, but it complicates logic and decreases readability. Two separate loops are better for clarity.

Mallikarjuna Mallisetty, author
About the author

Mallikarjuna Mallisetty

General programming

Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.

View LinkedIn profile ↗