X Shape Number Pattern in C++

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

What You’ll Learn

How to print a pattern that places numbers on two diagonals to form an X-like shape:

1 1,  2    2,   3  3,    4 4,     5.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1       1
 2     2
  3   3
   4 4
    5
1

Complete C++ Program

Print a number when the column matches the row index (left diagonal) or the mirrored index (right diagonal). Otherwise print spaces.

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";
    }

    return 0;
}

🧠 How It Works

1

Outer loop chooses the row

i runs from 1 to 5, producing 5 lines of output.

Rows
2

First inner loop prints the left diagonal

For columns j = 1..5, print j only when i == j. Otherwise print a space.

Left diagonal
3

Second inner loop prints the right diagonal

Loop k from 4 down to 1 and print k only when i == k. Otherwise print spaces to keep alignment.

Right diagonal
4

Combined row forms an X

The two diagonals move toward the center, so the gap shrinks on each next row.

Shape
=

Diagonal placement pattern

This is a good exercise for understanding how row/column indices map to positions.

2

Variation — User Input Version

Let the user choose the size (rows). This version prints the same X shape for any positive 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 = 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";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print stars instead of numbers to create an X star pattern
  • Use a constant width font (console) for perfect alignment
  • Print both diagonals in a single loop using j == i or j == rows - i + 1
  • Add spaces between the two halves to widen the X
  • Increase rows to make the shape larger

Avoid

  • Using proportional fonts when you care about column alignment
  • Printing extra newlines inside the inner loops
  • Forgetting to print spaces for non-diagonal positions
  • Hard-coding 5 if you want a reusable version

Key Takeaways

1

Diagonal checks like i == j help place characters at specific positions.

2

Printing spaces is essential to keep the X shape aligned.

3

The second loop mirrors the first half to form the right diagonal.

4

This pattern is a practical way to practice row/column thinking with nested loops.

❓ Frequently Asked Questions

The first loop prints the left half (including the left diagonal). The second loop prints the mirrored right half to create the other diagonal.
Yes. For width rows, you can print a number when j == i or j == rows - i + 1.
Add extra spaces between the two halves, or increase the total column count by changing the loop bounds.
Yes, when you mirror the columns properly (using rows and rows-1) the diagonals meet in the center, forming a symmetric X.

Explore More C++ Number Patterns!

Diagonal patterns help you think in coordinates (row/column), which is useful for grids and matrices too.

All Number Patterns →
Did you know?

You can print both diagonals in a single width loop using the condition j == i or j == rows - i + 1. That’s a common trick for X patterns.

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