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
Hollow Hourglass Star Pattern output
Output (n = 5):
*********
* *
* *
* *
*
* *
* *
* *
*********
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.
- Run the outer loop for
ifrom1ton, one row per pass (upper half: hollow inverted pyramid). - Go through
2 × n − ipositions in the row. Print stars wherei = 1 or j = i or j = 2 × n − i; print spaces everywhere else. - Move to the next line.
- Run the outer loop for
ifrom2ton, one row per pass (lower half: hollow pyramid). - Go through
n + i − 1positions in the row. Print stars wherei = n or j = n − i + 1 or j = n + i − 1; print spaces everywhere else. - Move to the next line.
Key takeaways
- The outline is two hollow triangles joined at their tips.
- Edges are where
jequals 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.











