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.
Approach
How to Solve It
Start with fixed max = 9, then generalize with scanf — optionally space the digits for wide rows.
Method
Idea
Best for
Three parts + 0
Ascend i..max, print 0, descend max..i
Learning, interviews, exams
Spaced digits
Same 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
Goal
Pattern
Grow rows downward
for (i = max + 1; i >= 1; i--)
Left half i..max
for (j = i; j <= max; j++) printf("%d", j);
Fixed center
printf("0");
Right half max..i
for (k = max; k >= i; k--) printf("%d", k);
End the row
printf("\n");
Spaced variant
printf("%d ", j); and printf("0 ");
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d", j) / printf("0")
Stays on the same line
Digits and the center zero
printf("\n")
Ends the current line
After all three parts
Print the whole mirror without a newline, then end the row once.
Try it
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 resultmax 5 · 6 rows · 36 digits
0
505
45054
3450543
234505432
12345054321
Trace
Worked Walkthrough — max = 4
Trace each outer-loop value of i from 5 down to 1 and list the three parts of the row.
i
Left i..max
Center
Right max..i
Printed row
5
(none)
0
(none)
0
4
4
0
4
404
3
34
0
43
34043
2
234
0
432
2340432
1
1234
0
4321
123404321
Total digits for max n = (n + 1)² (here 5² = 25). That square count is why time is O(n²).
Code
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;
}
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;
}
Output (when user enters 4)
Enter max digit (1-9): 4
0
404
34043
2340432
123404321
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;
}
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.