Complete Duplicate Number X Pattern in C

What You’ll Learn
How to print a complete X pattern where each row (except the top and bottom) prints the same number twice. We generate an upper half and then mirror it for the lower half.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
2 2
3 3
4 4
5 5
4 4
3 3
2 2
1Complete C Program
The first part prints i=1..rows. The second part prints i=rows-1..1 to mirror the top.
#include <stdio.h>
int main() {
int i, j, k;
for (i = 1; i <= 5; i++) {
for (j = 5; j >= 1; j--) {
if (i == j)
printf("%d", j);
else
printf(" ");
}
for (k = 2; k <= 5; k++) {
if (i == k)
printf("%d", k);
else
printf(" ");
}
printf("\n");
}
for (i = 4; i >= 1; i--) {
for (j = 5; j >= 1; j--) {
if (i == j)
printf("%d", j);
else
printf(" ");
}
for (k = 2; k <= 5; k++) {
if (i == k)
printf("%d", k);
else
printf(" ");
}
printf("\n");
}
return 0;
}🧠 How It Works
Upper half
Print rows from 1 up to 5 (the center row).
Two diagonals per row
The first inner loop prints the left diagonal when i == j. The second prints the right diagonal when i == k.
Mirror for the lower half
Print rows from 4 down to 1 so the center row is not duplicated.
Complete X
The bottom half is a mirror of the top half.
Variation — User Input Version
Read the center size (rows) using scanf().
#include <stdio.h>
int main() {
int i, j, k;
int rows;
printf("Enter the number of rows (center value): ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = rows; j >= 1; j--) {
if (i == j)
printf("%d", j);
else
printf(" ");
}
for (k = 2; k <= rows; k++) {
if (i == k)
printf("%d", k);
else
printf(" ");
}
printf("\n");
}
for (i = rows - 1; i >= 1; i--) {
for (j = rows; j >= 1; j--) {
if (i == j)
printf("%d", j);
else
printf(" ");
}
for (k = 2; k <= rows; k++) {
if (i == k)
printf("%d", k);
else
printf(" ");
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Replace numbers with
*to draw a classic X - Use two spaces per cell to make a wider X
- Print a thicker X by printing near the diagonals too
Avoid
- Starting the lower half at
rows(duplicates the center row) - Using
rows <= 0without validation
Key Takeaways
The complete shape is built by printing an upper half and mirroring it.
Two inner loops print the left and right diagonals.
Start the lower half at rows-1 to avoid duplicating the center row.
Time complexity is O(n²).
❓ Frequently Asked Questions
i=1.rows, the total is 2*rows - 1.Explore More C Number Patterns!
Keep practicing diagonal and symmetry patterns with new variations.
For size rows, this pattern prints 2*rows - 1 lines and has a single center row.
12 people found this page helpful
