Hollow Number Diamond in C++

What You’ll Learn
How to print a hollow number diamond in C++. The top half is a hollow pyramid (1 to rows) and the bottom half mirrors it back down to 1.
This pattern is ideal for learning how to combine nested loops with conditions to print either a digit or a space.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1Complete C++ Program
We print the top half (1..rows) and then the bottom half (rows-1..1). Each half uses the same diagonal-print logic.
#include <iostream>
using namespace std;
int main() {
int i, j, k;
int rows = 5;
// First Part (top half)
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";
}
// Second Part (bottom half)
for (i = rows - 1; i >= 1; 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
Split into two halves
The diamond is created by printing a top half (i = 1..rows) and a bottom half (i = rows-1..1).
Left diagonal printing
Loop j from rows down to 1. Print j only when i == j; otherwise print spaces.
Right diagonal printing
Loop k from 2 to rows. Print k only when i == k; otherwise print spaces.
Mirror the top half
The bottom half uses the same logic but runs i downward. That creates a symmetric diamond shape.
Hollow number diamond
Because we scan across positions with nested loops, the runtime grows roughly like O(n²) for n rows.
Variation — User Input Version
Use cin to choose the size of the diamond at runtime.
#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";
}
for (i = rows - 1; i >= 1; 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 debug alignment - Print
*to create a hollow star diamond - Print row numbers with a trailing space for consistent width (e.g.,
cout << i << ' ') - Build a filled diamond by printing numbers across the full width
- Wrap top/bottom printing into a function for reuse
Avoid
- Hardcoding row counts throughout the loops
- Forgetting the
rows - 1start for the bottom half (it would duplicate the middle row) - Mixing tabs and spaces (console output can shift)
- Using
endlrepeatedly in loops (slower)
Key Takeaways
Make a diamond by printing a pyramid up and then down.
Use conditions to print numbers only on the diagonal edges.
Start the bottom half at rows - 1 to avoid duplicating the middle row.
This technique generalizes to many shapes, including diamonds and hollow pyramids.
❓ Frequently Asked Questions
rows line, so the bottom half begins at rows - 1.*.Explore More C++ Number Patterns!
Keep practicing with hollow, filled, and symmetric patterns to master loops.
Many diamond patterns are made by combining two triangles or pyramids. Once you recognize the halves, you can build new shapes quickly by reusing the same loop logic.
12 people found this page helpful
