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 digits rising toward a max on the left, a fixed 0 in the middle, and the same digits falling on the right — growing longer each row.

Remember
Rule: i..max + 0 + max..i  (i = max+1..1)

0
909
89098
7890987
… 
1234567890987654321   ← max = 9

In C# the outer loop counts down from max + 1, the first inner loop prints i..max, then Write("0"), then the second inner loop prints max..i. When i = max + 1, both side loops are empty — so the first row is just 0.

How to Solve It

One three-part idea with two useful variants — packed digits and spaced digits.

MethodIdeaBest for
Ascend + 0 + descendPrint i..max, then 0, then max..iLearning, interviews, exams
Spaced digitsSame loops; print each value with a trailing spacePractice variants

Pseudocode

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

Cheat sheet

GoalPattern
Grow each rowfor (i = max + 1; i >= 1; i--)
Left halffor (j = i; j <= max; j++) Console.Write(j);
Center zeroConsole.Write("0");
Right mirrorfor (k = max; k >= i; k--) Console.Write(k);
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit and the center 0
Console.WriteLineEnds the current lineAfter both side loops

Live Preview

Change the max digit and the 0-centered mirror updates instantly — including the row count.

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
0
505
45054
3450543
234505432
12345054321

Worked Walkthrough — max = 4

Trace each outer-loop value of i: left digits, center 0, and right mirror.

iLeft / 0 / rightPrinted row
5— / 0 / —0
44 / 0 / 4404
334 / 0 / 4334043
2234 / 0 / 4322340432
11234 / 0 / 4321123404321

There are always max + 1 rows — one lone 0, then max growing mirrors.

C# Programs

Three complete programs: fixed max 9, custom max with input, and spaced digits. Use View Output for sample results.

Example 1 — Fixed max = 9

Count down from 10; print left digits, a center 0, then the mirror.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j, k;

            for (i = 10; i >= 1; i--)
            {
                for (j = i; j < 10; j++)
                {
                    Console.Write(j);
                }

                Console.Write("0");

                for (k = 9; k >= i; k--)
                {
                    Console.Write(k);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Start above max. When i = 10, both side loops skip — only 0 prints.

2. Grow both sides. As i falls, left prints i..9 and right prints 9..i.

3. Newline. WriteLine() after both loops starts the next longer row.

Example 2 — User Input

Read the max digit (1–9). Prefer int.TryParse so bad input does not throw.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int max;
            int i, j, k;

            Console.Write("Enter max digit (1-9): ");
            if (!int.TryParse(Console.ReadLine(), out max) || max < 1 || max > 9)
            {
                Console.WriteLine("Please enter a whole number from 1 to 9.");
                return;
            }

            for (i = max + 1; i >= 1; i--)
            {
                for (j = i; j <= max; j++)
                {
                    Console.Write(j);
                }

                Console.Write("0");

                for (k = max; k >= i; k--)
                {
                    Console.Write(k);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Validate max. Require a whole number from 1 to 9.

2. Same three parts. Outer loop starts at max + 1; left uses j <= max; right uses k >= i.

Example 3 — Spaced Digits

Same 0-centered mirror — only the print statements add spaces.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int max = 4;
            int i, j, k;

            for (i = max + 1; i >= 1; i--)
            {
                for (j = i; j <= max; j++)
                {
                    Console.Write(j + " ");
                }

                Console.Write("0 ");

                for (k = max; k >= i; k--)
                {
                    Console.Write(k + " ");
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same structure. Ascend, print zero, then descend — only the format changes.

2. Add gaps. Write(j + " "), Write("0 "), and Write(k + " ") insert spaces.

Edge Cases & Pitfalls

Check these before calling the solution done.

missing 0

Skip Write("0")

Without the center zero, left and right digits concatenate with no fixed middle.

wrong start

i = max instead of max + 1

You lose the lone 0 row. Start at max + 1.

WriteLine early

WriteLine between the side loops

That splits the mirror across two lines. Call WriteLine() only after both sides finish.

max = 1

Two rows

Output is 0 then 101 — a good sanity check.

max > 9

Multi-digit values

Digits above 9 print as two characters and break the visual mirror. Clamp to 1–9.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
Packed digits (Examples 1–2)O(n²)O(1)
Spaced digits (Example 3)O(n²)O(1)

There are n + 1 rows; each prints up to about 2n + 1 characters, so total work is O(n²).

Key Takeaways

  • Rule: print i..max, then 0, then max..i.
  • Start at max + 1: that is what creates the lone 0 first row.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after both sides.
  • Next step: Program 29 prints a mirrored 1..i .. 1 pattern.

One line: for i = max+1..1, print i..max, then 0, then max..i.

Frequently Asked Questions

Console.Write("0") sits between the ascending and descending loops, creating a fixed center on every row.
When i = max + 1, both side loops are empty — only 0 is printed.
Starting one above the max digit makes the first row a lone 0, then each step down adds more digits on both sides.
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 Console.Write(j + " ") and Console.Write(k + " ") in the loops — see Example 3.
O(n²) for max digit n because each row prints O(n) digits and there are O(n) rows.
Prefer int.TryParse(Console.ReadLine(), out max) and clamp to 1..9.
Two rows: 0 and 101 — the smallest non-trivial mirror with a zero center.

Did you know?

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

Next: Mirrored Number Pattern

Print ascending digits then mirror them back down on each row.

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