C Diagonal Mirror Number Pattern

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A mirror diagonal pattern prints digit i on the main diagonal and again on a mirrored diagonal. Spaces fill every other column, forming a V-shape that meets at the bottom tip.

Remember
Rule: left  j = 1..rows   → print i if i == j else space
      right k = rows-1..1 → print i if i == k else space

1       1
 2     2
  3   3
   4 4
    5     ← rows = 5

Follows the increasing-decreasing pyramid in Program 52; next is the full mirror diagonal diamond in Program 54.

How to Solve It

Use one outer loop and two inner loops. Left half tests i == j; right half tests i == k while counting k down from rows - 1 (skip the center column).

MethodIdeaBest for
Two halves + conditionsLeft i == j, right i == k, else spaceLearning, interviews, exams
Compact traceDry-run with rows = 3 before coding rows = 5Paper tracing and labs

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to rows:
        print i if i == j else a space
    for k from (rows - 1) down to 1:
        print i if i == k else a space
    print newline

Cheat sheet

GoalPattern
Pick each rowfor (i = 1; i <= rows; i++)
Left diagonalfor (j = 1; j <= rows; j++) printf(i == j ? "%d" : " ", j);
Right diagonalfor (k = rows - 1; k >= 1; k--) printf(i == k ? "%d" : " ", k);
Skip center twiceRight loop starts at rows - 1, not rows
Chars per row2 * rows - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j) / printf(" ")Stays on the same lineEach column in both halves
printf("\n")Ends the current lineAfter both inner loops

Print all columns without a newline, then end the row once.

Live Preview

Change the row count and the V-shape updates instantly — capped at 9 so every digit stays a single character.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 5×9 cells
1       1
 2     2 
  3   3  
   4 4   
    5    

Worked Walkthrough — Row i = 3, rows = 5

Trace where digit 3 lands on both diagonals.

HalfLoopWhen digit prints
Leftj = 1..5At j = 3 → 3 (spaces elsewhere)
Rightk = 4..1At k = 3 → 3 (spaces elsewhere)

Full row: 3 3 (9 characters). Each of n rows prints 2n - 1 chars → O(n²).

C Programs

Three complete programs: fixed rows = 5, scanf input, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — print digit when i == j or i == k, otherwise a space.

C
#include <stdio.h>

int main(void)
{
    int rows = 5;
    int i, j, k;

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= rows; j++)
            printf(i == j ? "%d" : " ", j);

        for (k = rows - 1; k >= 1; k--)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Left half. Scan j = 1..rows; print the digit only when i == j (main diagonal).

2. Right half. Scan k = rows-1..1; print when i == k — starting at rows - 1 skips the center column.

3. Bottom tip. On the last row, both diagonals meet — only one digit appears in the middle.

Example 2 — User Input Rows

Read rows with scanf and reject non-positive values.

C
#include <stdio.h>

int main(void)
{
    int rows;
    int i, j, k;

    printf("Enter the number of rows: ");
    if (scanf("%d", &rows) != 1 || rows <= 0)
    {
        printf("Please enter a positive integer.\n");
        return 1;
    }

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= rows; j++)
            printf(i == j ? "%d" : " ", j);

        for (k = rows - 1; k >= 1; k--)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input with a clear message.

2. Same core. Only the loop limits come from the user — both diagonal conditions stay identical.

3. Safer input tip. Cap demos to single-digit rows:

Safer input
if (scanf("%d", &rows) != 1 || rows < 1 || rows > 9)
{
    printf("Enter a whole number from 1 to 9.\n");
    return 1;
}

Example 3 — Compact rows = 3

Same two-loop structure with a smaller height for quick paper tracing.

C
#include <stdio.h>

int main(void)
{
    int rows = 3;
    int i, j, k;

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= rows; j++)
            printf(i == j ? "%d" : " ", j);

        for (k = rows - 1; k >= 1; k--)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Three rows. Wide V on row 1, closer digits on row 2, single tip on row 3.

2. Trace on paper. Confirm the right loop starts at 2 (not 3) so the center is not doubled.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = rows

Doubled center

If the right loop starts at k = rows, the middle column prints twice. Keep k = rows - 1.

print i always

No spaces

Printing i on every column fills the row with digits. Use the ternary: digit only when i == j / i == k.

\n inside

Broken rows

If printf("\n") sits inside either half, each column lands on its own line. Call the newline only after both loops.

rows = 1

Single tip

Output is just 1 — the right loop does not run. A good sanity check.

rows > 9

Multi-digit values

Past 9, values like 10 break column alignment. Cap demos at 9 or use fixed-width formatting.

scanf

Check the return value

If scanf fails, rows may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Each of n rows prints 2n - 1 characters → about 2n² prints. For n = 5 that is 45 characters.

Key Takeaways

  • Two diagonals: left uses i == j, right uses i == k, else space.
  • Skip the center twice: right loop starts at rows - 1 so the middle column is not doubled.
  • Break the row: call printf("\n") only after both halves.
  • Complexity: O(n²) time from n × (2n - 1) characters; O(1) extra space.

One line: for each i, print digit on i == j and i == k, spaces elsewhere, then printf("\n").

Frequently Asked Questions

The mirrored half prints rows-1 positions to avoid duplicating the center column. On the final row, only the main diagonal digit remains.
It prints the row number on the main diagonal (left) and on a mirrored diagonal (right), creating a symmetric V-shape like 1..5 on both sides.
The first loop prints the left half across rows columns. The second prints the right mirrored half across rows-1 columns in reverse.
Skipping the center column prevents printing the middle digit twice on rows where i equals the center index.
Change rows or read it from user input with scanf — see Example 2.
O(n²) for n rows because each row prints about 2n-1 characters using nested loops.
Program 52 builds palindromic digit rows with m++ and m--. Program 53 uses spacing and i == j / i == k to place digits on mirrored diagonals.
One row prints a single 1 — the left loop prints at j = 1 and the right loop does not run.

Did you know?

Each row prints the row number on the main diagonal (left) and on a mirrored diagonal (right) using i == j and i == k. Row 3 shows 3 on both sides — about 2n-1 characters per row, so O(n²) total.

Next: Mirror Diagonal Diamond

Continue with the full diamond — V on top plus an inverted V below (1..5..1 on both diagonals).

Program 54 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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