Diamond X Number Pattern in C++

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:
1 1
2 2
3 3
4 4
5
4 4
3 3
2 2
1 1Complete C++ Program
Print the diagonal row logic for i=1..rows, then repeat for i=rows-1..1 to mirror the shape.
#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
Print the upper half
The first outer loop prints rows for i = 1..rows.
Left diagonal
For columns j = 1..rows, print the number only when i == j; otherwise print spaces.
Right diagonal (mirrored columns)
The second inner loop runs k = rows-1..1 and prints when i == k, forming the other diagonal.
Print the lower half
The second outer loop prints i = rows-1..1 to mirror the upper half without duplicating the middle row.
Diamond-like X
The two diagonals converge, meet at the center row, then diverge again.
Variation — User Input Version
This version prints the same mirrored diamond X for any positive rows:
#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 == iorj == rows - i + 1 - Increase
rowsto 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
The pattern uses diagonal conditions like i == j to place digits.
Printing the second half in reverse creates a mirrored diamond shape.
Spaces are essential to keep columns aligned.
You can generalize the size by replacing constants with rows and rows-1.
❓ Frequently Asked Questions
rows and print when j == i or j == rows - i + 1.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.
If you think in coordinates, the two diagonals correspond to col == row and col == (rows - row + 1). That view makes many diagonal patterns easier.
12 people found this page helpful
