Complete Duplicate Number X Pattern in C

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Symmetry + Loops

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:

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

Complete C Program

The first part prints i=1..rows. The second part prints i=rows-1..1 to mirror the top.

c
#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

1

Upper half

Print rows from 1 up to 5 (the center row).

Top
2

Two diagonals per row

The first inner loop prints the left diagonal when i == j. The second prints the right diagonal when i == k.

Diagonals
3

Mirror for the lower half

Print rows from 4 down to 1 so the center row is not duplicated.

Mirror
=

Complete X

The bottom half is a mirror of the top half.

2

Variation — User Input Version

Read the center size (rows) using scanf().

c
#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 <= 0 without validation

Key Takeaways

1

The complete shape is built by printing an upper half and mirroring it.

2

Two inner loops print the left and right diagonals.

3

Start the lower half at rows-1 to avoid duplicating the center row.

4

Time complexity is O(n²).

❓ Frequently Asked Questions

Because the right diagonal starts printing from 2, so it doesn’t print anything for i=1.
For size rows, the total is 2*rows - 1.
O(n²) for n rows.

Explore More C Number Patterns!

Keep practicing diagonal and symmetry patterns with new variations.

All Number Patterns →
Did you know?

For size rows, this pattern prints 2*rows - 1 lines and has a single center row.

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