X Shape Number Pattern in C++

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:
1 1
2 2
3 3
4 4
5Complete C++ Program
Print a number when the column matches the row index (left diagonal) or the mirrored index (right diagonal). Otherwise print spaces.
#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
Outer loop chooses the row
i runs from 1 to 5, producing 5 lines of output.
First inner loop prints the left diagonal
For columns j = 1..5, print j only when i == j. Otherwise print a space.
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.
Combined row forms an X
The two diagonals move toward the center, so the gap shrinks on each next row.
Diagonal placement pattern
This is a good exercise for understanding how row/column indices map to positions.
Variation — User Input Version
Let the user choose the size (rows). This version prints the same X shape for any positive 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 = 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 == iorj == rows - i + 1 - Add spaces between the two halves to widen the X
- Increase
rowsto 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
Diagonal checks like i == j help place characters at specific positions.
Printing spaces is essential to keep the X shape aligned.
The second loop mirrors the first half to form the right diagonal.
This pattern is a practical way to practice row/column thinking with nested loops.
❓ Frequently Asked Questions
rows, you can print a number when j == i or j == rows - i + 1.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.
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.
12 people found this page helpful
