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.
Approach
How to Solve It
One three-part idea with two useful variants — packed digits and spaced digits.
Method
Idea
Best for
Ascend + 0 + descend
Print i..max, then 0, then max..i
Learning, interviews, exams
Spaced digits
Same loops; print each value with a trailing space
Practice 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
Goal
Pattern
Grow each row
for (i = max + 1; i >= 1; i--)
Left half
for (j = i; j <= max; j++) Console.Write(j);
Center zero
Console.Write("0");
Right mirror
for (k = max; k >= i; k--) Console.Write(k);
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit and the center 0
Console.WriteLine
Ends the current line
After both side loops
Try it
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 resultmax 5 · 6 rows
0
505
45054
3450543
234505432
12345054321
Trace
Worked Walkthrough — max = 4
Trace each outer-loop value of i: left digits, center 0, and right mirror.
i
Left / 0 / right
Printed row
5
— / 0 / —
0
4
4 / 0 / 4
404
3
34 / 0 / 43
34043
2
234 / 0 / 432
2340432
1
1234 / 0 / 4321
123404321
There are always max + 1 rows — one lone 0, then max growing mirrors.
Code
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();
}
}
}
}
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();
}
}
}
}
Output (when user enters 4)
0
404
34043
2340432
123404321
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();
}
}
}
}
Output
0
4 0 4
3 4 0 4 3
2 3 4 0 4 3 2
1 2 3 4 0 4 3 2 1
How It Works
1. Same structure. Ascend, print zero, then descend — only the format changes.
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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²).
Remember
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.