How do I print an hourglass pattern using loops in Java?

You can print an hourglass pattern by combining a shrinking, centered triangle with a growing one. Nested for loops control the leading spaces and stars on each row. Print the narrowest row only once so the two halves meet at a single center star.

Source code

Save this example as HourglassPatternExample.java. It uses a Java 25 compact source file with void main() and the IO console methods. Use JDK 25 or later; no preview flags or external libraries are required.

void main() {
    int halfHeight = 5;

    for (int row = halfHeight; row >= 1; row--) {
        for (int space = 0; space < halfHeight - row; space++) {
            IO.print(" ");
        }
        for (int star = 0; star < 2 * row - 1; star++) {
            IO.print("*");
        }
        IO.println();
    }

    for (int row = 2; row <= halfHeight; row++) {
        for (int space = 0; space < halfHeight - row; space++) {
            IO.print(" ");
        }
        for (int star = 0; star < 2 * row - 1; star++) {
            IO.print("*");
        }
        IO.println();
    }
}

Explanation

The variable halfHeight sets the number of rows from the top edge through the center, including the center row. For a positive value n, the complete hourglass has 2 * n - 1 rows. A value of five therefore produces nine rows.

The first outer loop counts down from halfHeight to 1. For each value of row, it prints halfHeight - row leading spaces followed by 2 * row - 1 stars. The number of stars decreases by two per row, while the number of leading spaces increases by one.

With halfHeight = 5, the upper half looks like this:

Row counter Leading spaces Stars
5 0 9
4 1 7
3 2 5
2 3 3
1 4 1

The second outer loop reverses that progression. It starts at row = 2 and counts up to halfHeight, producing 3, 5, 7, and 9 stars. Starting at 2 avoids printing the single-star center row twice.

Both halves use the same spacing formulas. As the lower half grows, its leading-space count decreases, keeping the stars centered around the same column.

Each leading space and each star occupies one character position. IO.print() keeps writing on the current line, and IO.println() ends the row after both inner loops finish. The IO class belongs to java.lang, so no explicit import is needed.

Run the example

With JDK 25 or later installed, open a terminal in the directory containing the source file and run it directly:

java HourglassPatternExample.java

You can also compile the source file explicitly and then run the compiled program:

javac HourglassPatternExample.java
java HourglassPatternExample

Output

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

Display the output in a monospaced font to preserve alignment. Leading spaces are part of the pattern.

Try it yourself

Change halfHeight to 3, then to 7. Before running each version, calculate the total height and the number of stars in the widest row. Both are 2 * halfHeight - 1 for a positive value.

Try halfHeight = 1. The first outer loop prints one star, and the second outer loop does not execute. A zero or negative value produces no output because neither outer loop executes.

Change the second outer loop to start at row = 1. Observe how repeating the center row changes the shape and adds one line to the output.

Finally, compare this pattern with How do I print a diamond pattern using loops in Java?. A diamond grows toward its center and then shrinks; the hourglass shrinks toward its center and then grows.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.