Java Mirrored Number Pattern (Spaced)

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A spaced mirror number pattern prints left digits 1..i and right digits i..1 on the same row, with spaces filling unused columns so both halves stay aligned.

Remember
Rule: for i from 1 to rows,
      left:  j = 1..rows → print j if j ≤ i else space
      right: k = rows..1 → print space if k > i else k

1        1
12      21
123    321
1234  4321
1234554321     ← rows = 5

Unlike Program 27 (tight palindrome, no gap spaces) and Program 28 (0-centered descending mirror), here both halves use fixed-width loops of size rows.

How to Solve It

Walk i from 1 to rows; print a fixed-width left half, then a fixed-width right half.

MethodIdeaBest for
Fixed-width if/elseLeft: digit if j ≤ i; right: space if k > iLearning, interviews, exams
Ternary formSame logic with j ≤ i ? j : " "Shorter code once the idea clicks

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to rows:
        print (j if j <= i else " ")
    for k from rows down to 1:
        print (" " if k > i else k)
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Left digit / spaceif (j <= i) System.out.print(j); else System.out.print(" ");
Right space / digitif (k > i) System.out.print(" "); else System.out.print(k);
End the rowSystem.out.println();
Ternary leftSystem.out.print(j <= i ? j : " ");

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit or space
System.out.printlnEnds the current lineAfter both inner loops

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

Live Preview

Change the row count and the spaced mirror updates instantly.

Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · width 10
1        1
12      21
123    321
1234  4321
1234554321

Worked Walkthrough — rows = 5

Trace left half, right half, and the gap for each i. Gap spaces = 2 × (rows − i).

iLeftGapRightPrinted row
11····8····11 1
212···6···2112 21
3123··4··321123 321
41234·2·43211234 4321
5123450543211234554321

Each row prints 2 × rows characters — total work is O(n²) for n rows.

Java Programs

Three complete programs: fixed rows = 5, Scanner input with ternary form, and a compact dry-run. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded width — if/else in both fixed-width inner loops.

Java
public class MirroredNumber {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= 5; j++) {
                if (j <= i)
                    System.out.print(j);
                else
                    System.out.print(" ");
            }
            for (int k = 5; k >= 1; k--) {
                if (k > i)
                    System.out.print(" ");
                else
                    System.out.print(k);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer loop ascends. i runs from 1 to 5 — one mirrored row per iteration.

2. Left half. Print j while j ≤ i; otherwise print a space to keep width 5.

3. Right half. Print a space while k > i; otherwise print k down to 1.

When i = 3: 123·· + ··321 → 123 321. When i = 5: both halves fill — 1234554321.

Example 2 — Rows Input

Read rows at runtime. Ternary operators replace if/else; both loops use rows as width.

Java
import java.util.Scanner;

public class MirroredNumberInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter rows: ");
        int rows = sc.nextInt();
        if (rows < 1) return;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows; j++)
                System.out.print(j <= i ? j : " ");

            for (int k = rows; k >= 1; k--)
                System.out.print(k > i ? " " : k);

            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and guard. Read rows; exit early if it is less than 1.

2. Generalize bounds. Both inner loops use rows instead of hard-coded 5.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive integer.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive integer.");
    return;
}

Example 3 — Compact rows = 3

Same if/else structure as Example 1 — smaller size for paper tracing.

Java
public class MirroredNumberSmall {
    public static void main(String[] args) {
        int rows = 3;

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows; j++) {
                if (j <= i) System.out.print(j);
                else System.out.print(" ");
            }
            for (int k = rows; k >= 1; k--) {
                if (k > i) System.out.print(" ");
                else System.out.print(k);
            }
            System.out.println();
        }
    }
}

How It Works

1. Same structure. Fixed-width left and right loops — only rows changes from 5 to 3.

2. Gap shrinks. Row 1 has 4 gap spaces; row 2 has 2; row 3 joins with none.

3. Dry-run first. Trace i = 1..3 on paper before coding the full rows = 5 demo.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Column of digits

If println is inside either inner loop, each character lands on its own line. Use print for digits/spaces; println only after both halves.

Wrong right test

Inverted mirror

Using k ≤ i for spaces on the right (instead of k > i) inverts the mirror half.

Skip spaces

Collapsed halves

Omitting the else branch that prints spaces collapses the pattern into a tight palindrome.

Mismatched width

Broken columns

Both inner loops must run exactly rows times — mismatched bounds misalign columns.

rows = 1

Single join

Output is 11 — one digit each side with no gap.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
Spaced mirror (Examples 1–3)O(n²)O(1)

n rows, each printing 2n characters — still quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: for each i, left prints 1..i (pad spaces), right prints i..1 (pad spaces).
  • Fixed width: both loops always run rows times so columns stay aligned.
  • Break the row: call println only after both inner loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: left 1..i + spaces, right spaces + i..1, then println().

Frequently Asked Questions

Spaces keep the left and right halves aligned so the pattern looks symmetric. Without them, the right half shifts left each row.
The right loop runs k from rows down to 1. When k > i it prints a space; otherwise it prints k — building i..1 on the right.
Both inner loops always run rows times. Extra positions are filled with spaces so columns stay aligned.
Program 27 prints a tight palindrome with no alignment spaces. Program 29 uses fixed-width loops and spaces for a spaced mirror.
When i equals rows, both halves fill all columns — 12345 on the left and 54321 on the right meet with no space between.
Replace 5 with rows in both inner loop bounds — see Example 2.
O(n²) for n rows because each row runs two inner loops of width n.
Use sc.hasNextInt() before sc.nextInt(), or wrap nextInt in try/catch — see Example 2 notes.

Did you know?

This pattern prints an increasing left half (1..i), then a mirrored right half (i..1). Spaces in the fixed-width loops keep both halves aligned until the final row joins without a gap.

Next: Right-Aligned Descending Triangle

Move on to the right-aligned descending number triangle in the Java number-pattern series.

Program 30 tutorial →

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