Number Pyramid Diamond in C++

What You’ll Learn
How to print a diamond-style number pyramid where rows grow by odd lengths:
1, 123, 12345, ..., up to 123456789, then shrink back down.
This pattern is built by printing an upper pyramid and then a lower pyramid.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
123
12345
1234567
123456789
1234567
12345
123
1Complete C++ Program
The first loop prints the upper half (1..5). The second loop prints the lower half (4..1). Each row prints numbers from 1 up to 2*i-1.
#include <iostream>
using namespace std;
int main() {
int i, j, k;
for (i = 1; i <= 5; i++) {
for (j = i; j < 5; j++)
cout << " ";
for (k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
for (i = 4; i >= 1; i--) {
for (j = 5; j > i; j--)
cout << " ";
for (k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
return 0;
}🧠 How It Works
Upper half (growing rows)
The first outer loop runs i = 1..rows and prints rows of length 2*i-1.
Leading spaces for centering
Before printing numbers, we print spaces so shorter rows are centered in the pyramid.
Print 1..(2*i-1)
The number loop prints k = 1..(2*i-1), producing 12345 and so on.
Lower half (shrinking rows)
The second outer loop runs i = rows-1..1 to mirror the top half.
Diamond pyramid
Row lengths sum to about n², so runtime is O(n²) for n rows.
Variation — User Input Version
Let the user choose the pyramid height (rows).
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
if (rows <= 0) return 0;
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++)
cout << " ";
for (int k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--)
cout << " ";
for (int k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Print descending numbers on the right side to form a palindromic pyramid
- Add spaces between digits for readability
- Use
rowsup to 9 for single-digit output, or format numbers with separators - Replace numbers with letters for an alphabet diamond
- Build each row in a string if you want finer control of spacing
Avoid
- Forgetting to mirror the bottom half (you won’t get a diamond)
- Using inconsistent spacing that breaks centering
- Printing huge row counts without considering alignment
- Using
endlinside loops unnecessarily
Key Takeaways
Each row prints 2*i-1 digits (odd-length rows).
Leading spaces center the pyramid.
Two outer loops (up then down) create the diamond.
Total output grows like n², so time is O(n²).
❓ Frequently Asked Questions
rows = 5, the widest row prints 2*5-1 = 9 digits: 123456789.cout << k << ' ' instead of cout << k, and adjust the leading spaces accordingly.i then decreasing numbers back down to 1 for each row.n^2.Explore More C++ Number Patterns!
Once you master odd-length rows, you can build pyramids, diamonds, and palindromes with ease.
The sum of the first n odd numbers is n². That’s why diamond pyramids like this often have a neat quadratic growth in total output.
12 people found this page helpful
