The hollow number pyramid draws only the outline of a pyramid, with each edge written in its row number: 1 at the top, then 2 2, 3 3, 4 4, and a full base of 5s.
Quick answer: Treat row i as n + i β 1 positions. Print the row number i on the two slanted edges, positions n β i + 1 and n + i β 1, and on every position of the last row; print a space everywhere else.
This program is part of the Pyramid Patterns collection.
Table of contents8 sections
Hollow Number Pyramid output
Output (n = 5):
1 2 2 3 3 4 4 555555555

Same shape, other characters: the Hollow Alphabet Pyramid and Hollow Pyramid Star Pattern programs draw 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 number pyramid works
This is the hollow pyramid with digits instead of stars. Row i of a pyramid covers the columns from n β i + 1 to n + i β 1; anything before that is indent. Inside the row only the two end positions are printed, except on the base, which is printed in full to close the shape. Writing the row number on each edge shows clearly which row each part of the outline belongs to.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
n + i β 1positions in the row. Print numbers (valuei) wherej > n β i and (i = n or j = n β i + 1 or j = n + i β 1); print spaces everywhere else. - Move to the next line.
Key takeaways
- Row
iruns from columnn β i + 1ton + i β 1. - Only the two ends of each row and the full base are printed.
- The condition
j > n β iskips the indent in front of the row. - Replace
iwith a star and you have the hollow pyramid star pattern.
Hollow 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 <= n + i - 1; j++) {
if (j > n - i && (i == n || j == n - i + 1 || j == n + i - 1))
printf("%d", i);
else
printf(" ");
}
printf("\n");
}
return 0;
}Hollow 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 <= n + i - 1; j++) {
if (j > n - i && (i == n || j == n - i + 1 || j == n + i - 1))
cout << i;
else
cout << " ";
}
cout << endl;
}
return 0;
}Hollow Number Pyramid in Java
public class HollowNumberPyramid {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n + i - 1; j++) {
if (j > n - i && (i == n || j == n - i + 1 || j == n + i - 1))
System.out.print(i);
else
System.out.print(" ");
}
System.out.println();
}
}
}Hollow Number Pyramid in Python
n = 5
for i in range(1, n + 1):
for j in range(1, n + i):
if j > n - i and (i == n or j == n - i + 1 or j == n + i - 1):
print(i, 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
Why is the base all 5s?
The last row is printed in full to close the shape, and every position on it prints the row number, which is 5. Print j on the base instead if you want 123456789.
How do I make the numbers on the base count?
Use a separate value for the last row, for example j β n + i, or print j there. The edges keep showing the row number.
Why test j > n β i at all?
Without it, the condition i == n would be checked for the leading spaces too. Keeping the indent out of the test makes the conditions easier to read and avoids printing digits in front of the row.






