C Mirrored Number Pattern (Spaced)

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

What Is This Pattern?

A spaced mirror prints left digits 1..i and right digits i..1 inside two fixed-width loops. Unused columns become spaces, so the gap shrinks until the last row joins with no gap.

Remember
Rule: for i from 1 to rows
      left  — digit if j <= i, else space  (j = 1..rows)
      right — space if k > i, else digit k (k = rows..1)

1        1
12      21
123    321
1234  4321
1234554321     ← rows = 5

Two coordinated halves share the same width. Follows the 0-centered mirror in Program 28; next is Program 30.

How to Solve It

Start with fixed rows = 5, then generalize with scanf — optionally demo a smaller count for tracing.

MethodIdeaBest for
Fixed-width + spacesLeft 1..i / spaces; right spaces / i..1Learning, interviews, exams
Ternary printSame loops; (cond) ? printf("%d", …) : printf(" ")Compact code after you know if/else

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to rows:
        if j <= i: print j
        else: print space
    for k from rows down to 1:
        if k > i: print space
        else: print k
    print newline

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Left half widthfor (j = 1; j <= rows; j++)
Left digit or spaceif (j <= i) printf("%d", j); else printf(" ");
Right half widthfor (k = rows; k >= 1; k--)
Right space or digitif (k > i) printf(" "); else printf("%d", k);
End the rowprintf("\n");

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j) / printf(" ")Stays on the same lineDigits and alignment spaces
printf("\n")Ends the current lineAfter both halves

Print the whole mirror without a newline, then end the row once.

Live Preview

Change the row count and the spaced mirror updates instantly — capped at 9 so every value 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 5 rows · 50 cells
1        1
12      21
123    321
1234  4321
1234554321

Worked Walkthrough — rows = 4

Trace each outer-loop value of i and list the left half, right half, and full printed row (spaces shown as · below for clarity).

iLeft (j = 1..4)Right (k = 4..1)Printed row
11······11 1
212····2112 21
3123··321123 321
41234432112344321

Each row prints 2 × rows characters. For n rows that is 2n² cells — why time is O(n²).

C Programs

Three complete programs: fixed rows, scanf input with ternary prints, and a small demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded row count — if/else in both fixed-width halves.

C
#include <stdio.h>

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

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j <= 5; j++)
        {
            if (j <= i)
                printf("%d", j);
            else
                printf(" ");
        }

        for (k = 5; k >= 1; k--)
        {
            if (k > i)
                printf(" ");
            else
                printf("%d", k);
        }

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop grows i. i runs from 1 to 5 — one mirrored row per pass.

2. Left half. j always runs 1..5; print the digit when j <= i, otherwise a space.

3. Right half. k runs 5..1; print a space when k > i, otherwise digit k.

4. Newline. printf("\n") ends the row after both halves.

When i = 1 you get 1 1; when i = 5 both halves fill — 1234554321.

Example 2 — User Input Version

Read rows with scanf and clamp to 1–9. Ternary operators compact the digit-or-space choice.

C
#include <stdio.h>

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

    printf("Enter rows (1-9): ");
    scanf("%d", &rows);
    if (rows < 1) rows = 1;
    if (rows > 9) rows = 9;

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and clamp. Read rows, then force it into 1..9 so single-digit columns stay valid.

2. Same two-half core. Only the hard-coded 5 becomes rows; ternaries replace if/else.

3. Safer input tip. Unchecked scanf leaves rows uninitialized on bad input. Prefer:

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 if/else logic with a smaller row count 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++)
        {
            if (j <= i)
                printf("%d", j);
            else
                printf(" ");
        }

        for (k = rows; k >= 1; k--)
        {
            if (k > i)
                printf(" ");
            else
                printf("%d", k);
        }

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only rows changes from 5 to 3 — both fixed-width halves stay identical.

2. Trace on paper. For i = 2: left prints 12, right prints 21 → 12 21.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for the classic five-row mirror.

Edge Cases & Pitfalls

Check these before calling the solution done.

no spaces

Missing middle gap

If you skip the space branches, the right half shifts left every row and the mirror collapses. Always fill unused columns.

short loops

Variable-width halves

Looping only to i (not rows) drops the padding spaces. Both inner loops must run the full width.

\n inside

Broken rows

If printf("\n") sits inside either inner loop, each character lands on its own line. Call the newline only after both halves.

rows > 9

Multi-digit values

printf("%d", 10) prints two characters and columns drift. Clamp demos to 1–9.

rows = 1

Smallest mirror

Output is 11 — both halves print a single 1 with no gap. A good sanity check.

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(rows²)O(1)
Small demo (Example 3)O(rows²)O(1)

Each of n rows prints 2n characters → 2n² cells total. Extra memory stays constant aside from loop counters.

Key Takeaways

  • Rule: left prints 1..i (else space); right prints i..1 (else space).
  • Fixed width: both inner loops always run to rows so columns stay aligned.
  • Break the row: call printf("\n") only after both halves.
  • Complexity: O(n²) time from 2n² cells; O(1) extra space.

One line: for each i, print a width-rows left half (1..i / spaces), then a width-rows right half (spaces / i..1), then printf("\n").

Frequently Asked Questions

Spaces keep the left and right halves aligned so the pattern looks symmetric. Without them, the right half shifts left each row.
The right loop runs k from rows down to 1. When k > i it prints a space; otherwise it prints k — building i..1 on the right.
Both inner loops always run rows times. Extra positions are filled with spaces so columns stay aligned.
Program 27 prints a tight palindrome with no alignment spaces. Program 29 uses fixed-width loops and spaces for a symmetric spaced mirror.
When i equals rows, both halves fill all columns — 12345 on the left and 54321 on the right meet with no space between.
Replace 5 with rows in both inner loop bounds — see Example 2.
O(n²) for n rows because each row runs two inner loops of width n.
Check scanf's return value and reject values outside 1..9 so single-digit columns stay aligned.

Did you know?

This pattern prints an increasing left half (1..i), then a mirrored right half (i..1). Fixed-width loops fill unused columns with spaces so both halves stay aligned until the final row joins with no gap.

Next: Right-Aligned Descending Triangle

Continue the C number-pattern series with a right-aligned descending triangle.

Program 30 tutorial →

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