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

Hollow Inverted Number Pyramid

The hollow inverted number pyramid draws the outline of an upside-down pyramid: a full top row of 5s, then two slanting edges that count down 4, 3, 2 and meet at a single 1.

Quick answer: Row i runs from column i to 2n βˆ’ i. Print the value n βˆ’ i + 1 on the whole first row and on the two ends of every other row; print spaces elsewhere. The value goes down by one each row, so the edges count 5, 4, 3, 2, 1.

This program is part of the Pyramid Patterns collection.

Table of contents8 sections
  1. Output
  2. How the hollow inverted number pyramid 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 Inverted Number Pyramid output

Output (n = 5):

555555555
 4     4
  3   3
   2 2
    1
Hollow Inverted Number Pyramid layout diagram, row by row (n = 5)
How each row of the hollow inverted number pyramid is built (n = 5). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Hollow Inverted Pyramid Star Pattern program draws this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.

How the hollow inverted number pyramid works

It mirrors the hollow number pyramid from top to bottom. In an inverted pyramid row i starts at column i and ends at 2n βˆ’ i, so those are the two edges. The first row is the wide side of the shape and is printed in full. Printing n βˆ’ i + 1 instead of i makes the numbers fall from 5 at the top to 1 at the point.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through 2 Γ— n βˆ’ i positions in the row. Print numbers (value n βˆ’ i + 1) where j β‰₯ i and (i = 1 or j = i or j = 2 Γ— n βˆ’ i); print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • Row i runs from column i to 2n βˆ’ i.
  • The top row is full; the others show only their two ends.
  • The value n βˆ’ i + 1 counts down from 5 to 1.
  • The test j >= i keeps the indent out of the shape.

Hollow Inverted Number Pyramid in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 2 * n - i; j++) {
            if (j >= i && (i == 1 || j == i || j == 2 * n - i))
                printf("%d", n - i + 1);
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Hollow Inverted Number Pyramid in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 2 * n - i; j++) {
            if (j >= i && (i == 1 || j == i || j == 2 * n - i))
                cout << n - i + 1;
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Hollow Inverted Number Pyramid in Java

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

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 2 * n - i; j++) {
                if (j >= i && (i == 1 || j == i || j == 2 * n - i))
                    System.out.print(n - i + 1);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Hollow Inverted Number Pyramid in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, 2 * n - i + 1):
        if j >= i and (i == 1 or j == i or j == 2 * n - i):
            print(n - i + 1, 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 do I put the 1 at the top instead?

Print i instead of n βˆ’ i + 1. The top row becomes 111111111 and the point at the bottom is 5.

How is it different from the hollow inverted pyramid of stars?

Only the character changes. The hollow inverted pyramid star pattern uses the same edge test with a star.

Why is row 4 not hollow?

Row 4 has three positions and the two ends are both printed, so there is only one space between them. The gap is bigger on the rows above.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 19 of 19 in Pyramid Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

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.

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.