Diamond X Number Pattern in C++

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

What You’ll Learn

How to print a diamond-like X pattern by printing an upper half and then mirroring it as a lower half.

This pattern places numbers on both diagonals while keeping other positions as spaces for alignment.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1       1
 2     2
  3   3
   4 4
    5
   4 4
  3   3
 2     2
1       1
1

Complete C++ Program

Print the diagonal row logic for i=1..rows, then repeat for i=rows-1..1 to mirror the shape.

C++
#include <iostream>
using namespace std;

int main() {
    int i, j, k;

    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= 5; j++) {
            if (i == j)
                cout << j;
            else
                cout << " ";
        }
        for (k = 4; k >= 1; k--) {
            if (i == k)
                cout << k;
            else
                cout << " ";
        }
        cout << "\n";
    }

    for (i = 4; i >= 1; i--) {
        for (j = 1; j <= 5; j++) {
            if (i == j)
                cout << j;
            else
                cout << " ";
        }
        for (k = 4; k >= 1; k--) {
            if (i == k)
                cout << k;
            else
                cout << " ";
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Print the upper half

The first outer loop prints rows for i = 1..rows.

Upper
2

Left diagonal

For columns j = 1..rows, print the number only when i == j; otherwise print spaces.

Diagonal
3

Right diagonal (mirrored columns)

The second inner loop runs k = rows-1..1 and prints when i == k, forming the other diagonal.

Mirror
4

Print the lower half

The second outer loop prints i = rows-1..1 to mirror the upper half without duplicating the middle row.

Lower
=

Diamond-like X

The two diagonals converge, meet at the center row, then diverge again.

2

Variation — User Input Version

This version prints the same mirrored diamond X for any positive rows:

C++
#include <iostream>
using namespace std;

static void printRow(int i, int rows) {
    for (int j = 1; j <= rows; j++) {
        if (i == j) cout << j;
        else cout << " ";
    }
    for (int k = rows - 1; k >= 1; k--) {
        if (i == k) cout << k;
        else cout << " ";
    }
    cout << "\n";
}

int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;

    if (rows <= 0) return 0;

    for (int i = 1; i <= rows; i++) printRow(i, rows);
    for (int i = rows - 1; i >= 1; i--) printRow(i, rows);

    return 0;
}

💡 Tips for Enhancement

Try These

  • Replace numbers with * to create a star diamond X
  • Use a single width loop with the condition j == i or j == rows - i + 1
  • Increase rows to make a larger diamond
  • Add an extra gap between the halves to widen the diamond
  • Print two-digit values with padding to keep alignment

Avoid

  • Repeating the middle row (start the second half at rows-1)
  • Using proportional fonts when checking alignment
  • Forgetting to print spaces in non-diagonal positions
  • Hard-coding loop bounds if you want a scalable solution

Key Takeaways

1

The pattern uses diagonal conditions like i == j to place digits.

2

Printing the second half in reverse creates a mirrored diamond shape.

3

Spaces are essential to keep columns aligned.

4

You can generalize the size by replacing constants with rows and rows-1.

❓ Frequently Asked Questions

Because the diagonals move inward until the center row, then outward as the pattern is mirrored back down.
The first pass prints the top half; the second pass prints the bottom half to mirror the shape.
Yes. Use width rows and print when j == i or j == rows - i + 1.
Use fixed-width formatting (like setw) so two-digit numbers take the same column width as single-digit numbers.

Explore More C++ Number Patterns!

Mirroring patterns is a common technique in pattern printing and helps strengthen your loop control.

All Number Patterns →
Did you know?

If you think in coordinates, the two diagonals correspond to col == row and col == (rows - row + 1). That view makes many diagonal patterns easier.

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