The hollow number triangle keeps only the edges of the number right triangle: the first column, the slanted side and the bottom row. Every number stays in the column it would have in the full triangle.
Quick answer: Use the normal triangle loops and test each position. Print j when it is on an edge, that is when j == 1, j == i or i == n, and print a space otherwise. Printing j rather than a fixed digit keeps every number in its own column.
This program is part of the Number Patterns collection.
Table of contents8 sections
Hollow Number Triangle output
Output (n = 5):
1 12 1 3 1 4 12345

Same shape, other characters: the Hollow Alphabet Triangle and Hollow Right Triangle 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 triangle works
A hollow shape is the solid shape with an extra test inside the inner loop. The three edges each have a simple condition: the left edge is the first position, the slanted edge is the last position of the row, and the bottom edge is the whole last row. Positions that pass none of the tests are inside the triangle and become spaces, which leaves a gap that grows by one each row.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
ipositions in the row. Print numbers (valuej) wherej = 1 or j = i or i = n; print spaces everywhere else. - Move to the next line.
Key takeaways
- A hollow pattern is the solid pattern plus an edge test.
- The three edges are
j == 1,j == iandi == n. - Printing
jkeeps each number in its own column, so the gaps line up. - The inside gap grows by one space per row until the full bottom row closes the shape.
Hollow Number Triangle in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n)
printf("%d", j);
else
printf(" ");
}
printf("\n");
}
return 0;
}Hollow Number Triangle in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n)
cout << j;
else
cout << " ";
}
cout << endl;
}
return 0;
}Hollow Number Triangle in Java
public class HollowNumberTriangle {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == n)
System.out.print(j);
else
System.out.print(" ");
}
System.out.println();
}
}
}Hollow Number Triangle in Python
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
if j == 1 or j == i or i == n:
print(j, 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 print j and not i on the edges?
With j, the column number, the bottom row reads 12345 and the slanted edge shows each row’s length. Printing i instead fills each edge with the row number: 1, 22, 3 3, 4 4, 55555.
How do I make a hollow number square?
Use n positions on every row and add j == n and i == 1 to the edge test. That gives the hollow number square.
Why is row 2 not hollow?
It only has two positions and both are edges, so there is no inside yet. The gap appears from row 3 on.






