A 0-centered descending mirror prints ascending digits, a fixed 0, then the same digits descending — growing longer as the outer start value decreases.
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
Unlike Program 27 (peak digit i in the middle), here the center is always 0 and the sides grow toward max.
Approach
How to Solve It
Descend i from max + 1; print left half, 0, then right half.
Method
Idea
Best for
Three parts
Ascend i..max, print 0, descend max..i
Learning, interviews, exams
Spaced digits
Same loops; print each value with a trailing space
When wide rows need gaps
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
Walk rows
for (int i = max + 1; i >= 1; i--)
Left half
for (int j = i; j <= max; j++) System.out.print(j);
Center
System.out.print("0");
Right half
for (int k = max; k >= i; k--) System.out.print(k);
End the row
System.out.println();
Clamp input
Keep max in 1..9
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit and the center 0
System.out.println
Ends the current line
After both side loops
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the max digit and the 0-centered mirror updates instantly.
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 left half, center, and right half for each i.
i
Left
Center
Right
Printed row
5
(empty)
0
(empty)
0
4
4
0
4
404
3
34
0
43
34043
2
234
0
432
2340432
1
1234
0
4321
123404321
There are max + 1 rows; each prints O(max) digits — total work is O(n²) for max digit n.
Code
Java Programs
Three complete programs: fixed max = 9, Scanner input with clamp, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed max = 9
Hard-coded max — outer loop from 10 down to 1; center is always 0.
Java
public class ZeroCenteredMirror {
public static void main(String[] args) {
for (int i = 10; i >= 1; i--) {
for (int j = i; j < 10; j++)
System.out.print(j);
System.out.print("0");
for (int k = 9; k >= i; k--)
System.out.print(k);
System.out.println();
}
}
}
1. Outer loop descends.i runs from 10 down to 1 — first row has empty sides.
2. Left then center. Print j from i to 9 (when j < 10), then print 0.
3. Mirror right. Print k from 9 down to i, then println.
When i = 9: 9 + 0 + 9 → 909. When i = 1: the full 1234567890987654321.
Example 2 — Max Input
Read max at runtime. Clamp to 1..9 so loop bounds stay valid.
Java
import java.util.Scanner;
public class ZeroCenteredMirrorInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter max digit (1-9): ");
int max = sc.nextInt();
if (max < 1) max = 1;
if (max > 9) max = 9;
for (int i = max + 1; i >= 1; i--) {
for (int j = i; j <= max; j++)
System.out.print(j);
System.out.print("0");
for (int k = max; k >= i; k--)
System.out.print(k);
System.out.println();
}
sc.close();
}
}
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.
2. Generalize bounds. Outer starts at max + 1; sides use max instead of hard-coded 9.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a digit from 1 to 9.");
return;
}
int max = sc.nextInt();
if (max < 1 || max > 9) {
System.out.println("Enter a digit from 1 to 9.");
return;
}
Example 3 — Spaced Digits
Keep max = 9 but print each value followed by a space (first four rows shown).
Java
public class ZeroCenteredMirrorSpaced {
public static void main(String[] args) {
int max = 9;
for (int i = max + 1; i >= 1; i--) {
for (int j = i; j <= max; j++)
System.out.print(j + " ");
System.out.print("0 ");
for (int k = max; k >= i; k--)
System.out.print(k + " ");
System.out.println();
}
}
}
Output (first 4 rows)
0
9 0 9
8 9 0 9 8
7 8 9 0 9 8 7
How It Works
1. Same structure. Left half, center 0, right half — identical bounds to Example 1.
2. Only the print changes. Trailing spaces make wide rows easier to read.
3. Same mirror. Digit order is unchanged — only spacing differs.
Edge Cases & Pitfalls
Check these before calling the solution done.
Start at max
Missing lone 0
Starting the outer loop at max instead of max + 1 skips the first single-0 row.
No center
Broken mirror
Forgetting print("0") glues the halves together without a center.
println inside
Column of digits
If println is inside either side loop, each digit lands on its own line. Use print for digits; println only after both sides.
max = 1
Two rows
Output is 0 then 101 — the smallest non-trivial case.
max > 9
Multi-digit
Values like 10 print as two characters and skew the visual mirror. Clamp to 1..9.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a digit from 1 to 9.
Analysis
Time and Space Complexity
Program
Time
Extra space
0-centered mirror (Examples 1–3)
O(n²)
O(1)
About n + 1 rows, each printing O(n) digits for max digit n — still quadratic.
Remember
Key Takeaways
Rule: for i from max + 1 down to 1, print i..max, then 0, then max..i.
Fixed center:print("0") between the two side loops on every row.
Break the row: call println only after both side loops.
Complexity:O(n²) time; O(1) extra space.
One line: print i..max, then 0, then max..i, then println().
Frequently Asked Questions
System.out.print("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 past 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 System.out.print(j + " ") and System.out.print(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.
Use sc.hasNextInt() before sc.nextInt(), then clamp max to 1..9 — see Example 2 notes.
🤔
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.