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

Number Hourglass Pattern

Read summary with AI tools

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
  1. Output
  2. How the number hourglass 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

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
Number Hourglass Pattern layout diagram, row by row (n = 5)
How each row of the number hourglass pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass (upper half).
  2. Print i − 1 spaces.
  3. Print n − i + 1 numbers (value i + j − 1).
  4. Move to the next line.
  5. Run the outer loop for i from n − 1 down to 1, one row per pass (lower half).
  6. Print i − 1 spaces.
  7. Print n − i + 1 numbers (value i + j − 1).
  8. Move to the next line.

Key takeaways

  • Every row prints the numbers from i to n; 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 − 1 so 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.

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.