The number hourglass pattern, also called the number sandglass, prints 1 to 5 on the top row and drops the smallest number on each row until only 5 is left, then builds the rows back up.
Quick answer: Row i starts with i − 1 spaces and then prints the numbers i to n, each followed by a space. Run the rows from 1 to n for the upper half and from n − 1 back down to 1 for the lower half, so the single 5 in the middle appears only once.
This program is part of the Tricky Patterns collection.
Table of contents8 sections
Number Hourglass Pattern output
Output (n = 5):
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
How the number hourglass pattern works
Each row is a slice of the same list, 1 to n, that starts one number later than the row above. Printing one space per missing number keeps the slices centred, because every number takes two characters: the digit and the space after it. The lower half replays the same rows in the opposite order.
- Run the outer loop for
ifrom1ton, one row per pass (upper half). - Print
i − 1spaces. - Print
n − i + 1numbers (valuei + j − 1). - Move to the next line.
- Run the outer loop for
ifromn − 1down to1, one row per pass (lower half). - Print
i − 1spaces. - Print
n − i + 1numbers (valuei + j − 1). - Move to the next line.
Key takeaways
- Every row prints the numbers from
iton; only the start moves. - One leading space per row keeps the centre, because each number is two characters wide.
- The lower half starts at
n − 1so the middle row is not repeated. - With numbers above 9 the shape skews; print each number in a fixed width to keep it straight.
Number Hourglass Pattern in C
#include <stdio.h>
int main(void) {
int n = 5;
// upper half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++)
printf(" ");
for (int j = 1; j <= n - i + 1; j++)
printf("%d ", i + j - 1);
printf("\n");
}
// lower half
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i - 1; j++)
printf(" ");
for (int j = 1; j <= n - i + 1; j++)
printf("%d ", i + j - 1);
printf("\n");
}
return 0;
}Number Hourglass Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
// upper half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++)
cout << " ";
for (int j = 1; j <= n - i + 1; j++)
cout << i + j - 1 << " ";
cout << endl;
}
// lower half
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i - 1; j++)
cout << " ";
for (int j = 1; j <= n - i + 1; j++)
cout << i + j - 1 << " ";
cout << endl;
}
return 0;
}Number Hourglass Pattern in Java
public class NumberHourglass {
public static void main(String[] args) {
int n = 5;
// upper half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i - 1; j++)
System.out.print(" ");
for (int j = 1; j <= n - i + 1; j++)
System.out.print((i + j - 1) + " ");
System.out.println();
}
// lower half
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= i - 1; j++)
System.out.print(" ");
for (int j = 1; j <= n - i + 1; j++)
System.out.print((i + j - 1) + " ");
System.out.println();
}
}
}Number Hourglass Pattern in Python
n = 5
# upper half
for i in range(1, n + 1):
for j in range(1, i):
print(" ", end="")
for j in range(1, n - i + 2):
print(i + j - 1, end=" ")
print()
# lower half
for i in range(n - 1, 0, -1):
for j in range(1, i):
print(" ", end="")
for j in range(1, n - i + 2):
print(i + j - 1, 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 star hourglass?
The shape is the same as the hourglass star pattern, but each position holds a number instead of a star, and the numbers tell you where each row starts.
How do I print the numbers counting down instead?
Print n − j + 1 for j from 1 to n − i + 1. The top row becomes 5 4 3 2 1 and the middle row is 1.
Why does the pattern drift for n = 12?
Two-digit numbers take three characters with their space, but the indent still adds only one space per row. Print every number as %3d and indent by three spaces per row, and the hourglass straightens out again.











