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
Hollow Inverted Number Pyramid output
Output (n = 5):
555555555
4 4
3 3
2 2
1
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.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
2 Γ n β ipositions in the row. Print numbers (valuen β i + 1) wherej β₯ i and (i = 1 or j = i or j = 2 Γ n β i); print spaces everywhere else. - Move to the next line.
Key takeaways
- Row
iruns from columnito2n β i. - The top row is full; the others show only their two ends.
- The value
n β i + 1counts down from 5 to 1. - The test
j >= ikeeps 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.






