Hollow Number Pyramid (Diagonal Print) in C++

What You’ll Learn
How to print a hollow number pyramid in C++ where each row prints the row number at two diagonal positions (left and right), and spaces everywhere else.
This is a useful exercise for learning nested loops + if/else conditions to decide what to print at each position.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
2 2
3 3
4 4
5 5Complete C++ Program
We scan across columns and print the number only when the current column matches the diagonal position for that row. Otherwise we print a space.
#include <iostream>
using namespace std;
int main() {
int i, j, k;
int rows = 5;
for (i = 1; i <= rows; i++) {
for (j = rows; j >= 1; j--) {
if (i == j)
cout << j;
else
cout << " ";
}
for (k = 2; k <= rows; k++) {
if (i == k)
cout << k;
else
cout << " ";
}
cout << "\n";
}
return 0;
}🧠 How It Works
Choose the row count
int rows = 5; controls how many lines are printed.
Outer loop prints rows
for (i = 1; i <= rows; i++) runs once per row and decides which number should appear on that row.
Left diagonal (count down columns)
for (j = rows; j >= 1; j--) prints the left side. When i == j, we print j; otherwise we print a space.
Right diagonal (count up columns)
for (k = 2; k <= rows; k++) prints the right side. When i == k, we print k; otherwise we print a space.
Hollow number pyramid
Only two positions per row print a number. Checking each position makes the runtime roughly O(n²) for n rows.
Variation — User Input Version
Let the user choose how many rows to print using cin.
#include <iostream>
using namespace std;
int main() {
int i, j, k;
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++) {
for (j = rows; j >= 1; j--) {
if (i == j)
cout << j;
else
cout << " ";
}
for (k = 2; k <= rows; k++) {
if (i == k)
cout << k;
else
cout << " ";
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Replace spaces with
.temporarily to visualize alignment while debugging - Print
*instead of numbers to create a hollow star pyramid - Print the full pyramid (fill inside) by removing the conditions
- Build a diamond by printing this pyramid and then reversing it
- Add input validation to reject non-positive row counts
Avoid
- Forgetting the newline after each row
- Hardcoding 5 in multiple places instead of using
rows - Using tabs inconsistently (they render differently in consoles)
- Printing
endlin tight loops (slower than"\n")
Key Takeaways
This is a hollow pattern: only diagonal positions print numbers.
Use conditional checks inside loops to decide between a number and a space.
Split printing into two halves (left and right) to keep logic simple.
The same idea works for other shapes like hollow stars.
❓ Frequently Asked Questions
1.if conditions and print the row number (or a sequence) for every position you want filled.Explore More C++ Number Patterns!
Learn more variations of hollow and filled pyramids, triangles, and diamonds.
Hollow patterns are great practice for condition-based printing. Once you master this, you can build framed rectangles, hollow diamonds, and more using the same idea.
12 people found this page helpful
