Number Pyramid Diamond in C++

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1
1

Complete 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.

C++
#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

1

Upper half (growing rows)

The first outer loop runs i = 1..rows and prints rows of length 2*i-1.

Top
2

Leading spaces for centering

Before printing numbers, we print spaces so shorter rows are centered in the pyramid.

Alignment
3

Print 1..(2*i-1)

The number loop prints k = 1..(2*i-1), producing 12345 and so on.

Digits
4

Lower half (shrinking rows)

The second outer loop runs i = rows-1..1 to mirror the top half.

Bottom
=

Diamond pyramid

Row lengths sum to about , so runtime is O(n²) for n rows.

2

Variation — User Input Version

Let the user choose the pyramid height (rows).

C++
#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 rows up 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 endl inside loops unnecessarily

Key Takeaways

1

Each row prints 2*i-1 digits (odd-length rows).

2

Leading spaces center the pyramid.

3

Two outer loops (up then down) create the diamond.

4

Total output grows like , so time is O(n²).

❓ Frequently Asked Questions

With rows = 5, the widest row prints 2*5-1 = 9 digits: 123456789.
Print cout << k << ' ' instead of cout << k, and adjust the leading spaces accordingly.
Yes. Print increasing numbers up to i then decreasing numbers back down to 1 for each row.
O(n²) for n rows because the total printed digits are proportional to n^2.

Explore More C++ Number Patterns!

Once you master odd-length rows, you can build pyramids, diamonds, and palindromes with ease.

All Number Patterns →
Did you know?

The sum of the first n odd numbers is . That’s why diamond pyramids like this often have a neat quadratic growth in total output.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful