New: 203 pattern programs with tested code in C, C++, Java and Python Explore patterns

Hollow Hourglass Star Pattern

Read summary with AI tools

The hollow hourglass star pattern, also called the hollow sandglass, keeps only the outline of the hourglass: a full row of stars at the top and bottom and two slanting edges that meet in the middle.

Quick answer: Print a hollow inverted pyramid for rows 1 to n, then a hollow pyramid for rows 2 to n. In both halves a position gets a star only if it is on a slanting edge, or if the row is the full top or bottom row; every other position is a space.

This program is part of the Tricky Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow hourglass star pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Hollow Hourglass Star Pattern output

Output (n = 5):

*********
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
*********
Hollow Hourglass Star Pattern layout diagram, row by row (n = 5)
How each row of the hollow hourglass star pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

How the hollow hourglass star pattern works

Think of the solid hourglass and then erase its inside. In the upper half, row i runs from column i to column 2n − i, so those two columns are the edges. In the lower half row i runs from n − i + 1 to n + i − 1. The lower half starts at row 2 so the single star in the middle is printed only once.

  1. Run the outer loop for i from 1 to n, one row per pass (upper half: hollow inverted pyramid).
  2. Go through 2 × n − i positions in the row. Print stars where i = 1 or j = i or j = 2 × n − i; print spaces everywhere else.
  3. Move to the next line.
  4. Run the outer loop for i from 2 to n, one row per pass (lower half: hollow pyramid).
  5. Go through n + i − 1 positions in the row. Print stars where i = n or j = n − i + 1 or j = n + i − 1; print spaces everywhere else.
  6. Move to the next line.

Key takeaways

  • The outline is two hollow triangles joined at their tips.
  • Edges are where j equals the first or last column of the row.
  • The top and bottom rows are printed in full, which closes the shape.
  • Starting the lower half at row 2 avoids printing the middle star twice.

Hollow Hourglass Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 5;

    // upper half: hollow inverted pyramid
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 2 * n - i; j++) {
            if (i == 1 || j == i || j == 2 * n - i)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    // lower half: hollow pyramid
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= n + i - 1; j++) {
            if (i == n || j == n - i + 1 || j == n + i - 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Hollow Hourglass Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    // upper half: hollow inverted pyramid
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 2 * n - i; j++) {
            if (i == 1 || j == i || j == 2 * n - i)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

    // lower half: hollow pyramid
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j <= n + i - 1; j++) {
            if (i == n || j == n - i + 1 || j == n + i - 1)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Hollow Hourglass Star Pattern in Java

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

        // upper half: hollow inverted pyramid
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 2 * n - i; j++) {
                if (i == 1 || j == i || j == 2 * n - i)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }

        // lower half: hollow pyramid
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= n + i - 1; j++) {
                if (i == n || j == n - i + 1 || j == n + i - 1)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Hollow Hourglass Star Pattern in Python

n = 5

# upper half: hollow inverted pyramid
for i in range(1, n + 1):
    for j in range(1, 2 * n - i + 1):
        if i == 1 or j == i or j == 2 * n - i:
            print("*", end="")
        else:
            print(" ", end="")
    print()

# lower half: hollow pyramid
for i in range(2, n + 1):
    for j in range(1, n + i):
        if i == n or j == n - i + 1 or j == n + i - 1:
            print("*", end="")
        else:
            print(" ", end="")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

How is this different from the hollow diamond?

The hollow diamond is wide in the middle and pointed at the top and bottom. The hourglass is the opposite: wide at the top and bottom and pointed in the middle. Swap the order of the two halves and you turn one into the other.

Why are the top and bottom rows full?

Without them the shape would be an X, open at both ends. The conditions i == 1 and i == n fill those two rows and turn the X into a glass.

How do I let the user choose the size?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

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