C Mirror Number Pattern (0-Centered)

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

What Is This Pattern?

A 0-centered descending mirror prints ascending digits, a fixed 0, then descending digits on each row — starting from a lone 0 and growing until both sides reach the max digit.

Remember
Rule: for i from max+1 down to 1
      print i..max, then 0, then max..i

0
909
89098
7890987
…
1234567890987654321     ← max = 9

Three coordinated pieces: left ascend, fixed center, right descend. Follows the palindrome triangle in Program 27; next is Program 29.

How to Solve It

Start with fixed max = 9, then generalize with scanf — optionally space the digits for wide rows.

MethodIdeaBest for
Three parts + 0Ascend i..max, print 0, descend max..iLearning, interviews, exams
Spaced digitsSame loops; printf("%d ", …) and printf("0 ")Easier reading on long rows

Pseudocode

Pseudocode
for i from max+1 down to 1:
    for j from i to max:
        print j (no newline)
    print 0
    for k from max down to i:
        print k (no newline)
    print newline

Cheat sheet

GoalPattern
Grow rows downwardfor (i = max + 1; i >= 1; i--)
Left half i..maxfor (j = i; j <= max; j++) printf("%d", j);
Fixed centerprintf("0");
Right half max..ifor (k = max; k >= i; k--) printf("%d", k);
End the rowprintf("\n");
Spaced variantprintf("%d ", j); and printf("0 ");

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j) / printf("0")Stays on the same lineDigits and the center zero
printf("\n")Ends the current lineAfter all three parts

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

Live Preview

Change the max digit and the 0-centered 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 max 5 · 6 rows · 36 digits
0
505
45054
3450543
234505432
12345054321

Worked Walkthrough — max = 4

Trace each outer-loop value of i from 5 down to 1 and list the three parts of the row.

iLeft i..maxCenterRight max..iPrinted row
5(none)0(none)0
4404404
33404334043
223404322340432
1123404321123404321

Total digits for max n = (n + 1)² (here 5² = 25). That square count is why time is O(n²).

C Programs

Three complete programs: fixed max, scanf input, and a spaced-digits variant. Use View Output to reveal sample results.

Example 1 — Fixed max = 9

Hard-coded max digit — outer loop from 10 down to 1; fixed 0 between both halves.

C
#include <stdio.h>

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

    for (i = 10; i >= 1; i--)
    {
        for (j = i; j <= 9; j++)
            printf("%d", j);

        printf("0");

        for (k = 9; k >= i; k--)
            printf("%d", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop shrinks i. i runs from 10 down to 1 — first row has empty sides.

2. Left half. j prints i..9 when that range is non-empty.

3. Fixed center. printf("0") always sits between the two halves.

4. Right half + newline. k prints 9..i; then printf("\n") ends the row.

When i = 10 you get 0; when i = 9 you get 909; when i = 1 you get the full mirror.

Example 2 — User Input Version

Read max with scanf and clamp to 1–9. Outer loop runs from max + 1 down to 1.

C
#include <stdio.h>

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

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

    for (i = max + 1; i >= 1; i--)
    {
        for (j = i; j <= max; j++)
            printf("%d", j);

        printf("0");

        for (k = max; k >= i; k--)
            printf("%d", k);

        printf("\n");
    }

    return 0;
}

How It Works

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

2. Same three-part core. Only the hard-coded 9/10 become max/max + 1.

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

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

Example 3 — Spaced digits

Keep max = 5; print each digit (and the center zero) followed by a space.

C
#include <stdio.h>

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

    for (i = max + 1; i >= 1; i--)
    {
        for (j = i; j <= max; j++)
            printf("%d ", j);

        printf("0 ");

        for (k = max; k >= i; k--)
            printf("%d ", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Outer countdown and both side loops are unchanged.

2. Add trailing spaces. Only the print statements change — including printf("0 ").

3. Learn the compact form first. Use Examples 1–2 for the classic no-space mirror; treat spacing as a polish option.

Edge Cases & Pitfalls

Check these before calling the solution done.

no zero

Missing center

Without printf("0"), the halves glue together and you lose the 0-centered mirror. Keep the center print between both loops.

i++

Upside-down growth

If the outer loop counts up instead of down, the longest row prints first. Use for (i = max + 1; i >= 1; i--).

\n inside

Broken rows

If printf("\n") sits inside either inner loop, each digit lands on its own line. Call the newline only after all three parts.

max > 9

Multi-digit values

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

max = 1

Smallest mirror

Output is 0 then 101 — a good sanity check.

scanf

Check the return value

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

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(max²)O(1)
Spaced digits (Example 3)O(max²)O(1)

Total digits = (max + 1)² — quadratic in max. For max = 9 that is 100 printed digits.

Key Takeaways

  • Rule: print i..max, then 0, then max..i.
  • Start above max: i = max + 1 gives the lone 0 row first.
  • Break the row: call printf("\n") only after all three parts.
  • Complexity: O(n²) time from (n + 1)² digits; O(1) extra space.

One line: for i from max + 1 down to 1, print i..max, then 0, then max..i, then printf("\n").

Frequently Asked Questions

printf("0") sits between the ascending and descending loops, creating a fixed center on every row.
When i = max + 1, both side loops are empty (j starts past max; k starts below i) — only 0 is printed.
Starting one above the max digit makes the first row empty on both sides, giving the single 0 row.
Program 27 mirrors 1..i on each row. Program 28 uses a fixed 0 center and grows digits toward max on both sides as i decreases.
Replace 9 with max and start i at max + 1 — see Example 2.
Use printf("%d ", j) and printf("%d ", k), plus printf("0 ") — see Example 3.
O(n²) for max digit n because total printed digits equal (n + 1)².
Check scanf's return value and clamp max to 1..9 so loop bounds stay valid.

Did you know?

This pattern prints ascending digits from i to max, a fixed 0 in the center, then descending digits from max down to i. As i decreases, each row grows into the long mirror 1234567890987654321.

Next: Spaced Mirror Number Pattern

Build a mirror with intentional spacing gaps between halves.

Program 29 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