Java Mirror Number Pattern (0-Centered)

Beginner
7 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 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.

How to Solve It

Descend i from max + 1; print left half, 0, then right half.

MethodIdeaBest for
Three partsAscend i..max, print 0, descend max..iLearning, interviews, exams
Spaced digitsSame loops; print each value with a trailing spaceWhen 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

GoalPattern
Walk rowsfor (int i = max + 1; i >= 1; i--)
Left halffor (int j = i; j <= max; j++) System.out.print(j);
CenterSystem.out.print("0");
Right halffor (int k = max; k >= i; k--) System.out.print(k);
End the rowSystem.out.println();
Clamp inputKeep max in 1..9

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit and the center 0
System.out.printlnEnds the current lineAfter both side loops

Print characters without a newline, then end the row once.

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 result max = 5 · 6 rows
0
505
45054
3450543
234505432
12345054321

Worked Walkthrough — max = 4

Trace left half, center, and right half for each i.

iLeftCenterRightPrinted row
5(empty)0(empty)0
4404404
33404334043
223404322340432
1123404321123404321

There are max + 1 rows; each prints O(max) digits — total work is O(n²) for max digit n.

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();
        }
    }
}

How It Works

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();
    }
}

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();
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Spaced Mirror Number Pattern

Move on to the spaced mirror number pattern in the Java number-pattern series.

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