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.
Approach
How to Solve It
Walk i from 1 to rows; print a fixed-width left half, then a fixed-width right half.
Method
Idea
Best for
Fixed-width if/else
Left: digit if j ≤ i; right: space if k > i
Learning, interviews, exams
Ternary form
Same 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
Goal
Pattern
Walk rows
for (int i = 1; i <= rows; i++)
Left digit / space
if (j <= i) System.out.print(j); else System.out.print(" ");
Right space / digit
if (k > i) System.out.print(" "); else System.out.print(k);
End the row
System.out.println();
Ternary left
System.out.print(j <= i ? j : " ");
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit or space
System.out.println
Ends the current line
After both inner loops
Print characters without a newline, then end the row once.
Try it
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 resultrows = 5 · width 10
1 1
12 21
123 321
1234 4321
1234554321
Trace
Worked Walkthrough — rows = 5
Trace left half, right half, and the gap for each i. Gap spaces = 2 × (rows − i).
i
Left
Gap
Right
Printed row
1
1····
8
····1
1 1
2
12···
6
···21
12 21
3
123··
4
··321
123 321
4
1234·
2
·4321
1234 4321
5
12345
0
54321
1234554321
Each row prints 2 × rows characters — total work is O(n²) for n rows.
Code
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();
}
}
}
Output
1 1
12 21
123 321
1234 4321
1234554321
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();
}
}
Output (when user enters 4)
Enter rows: 4
1 1
12 21
123 321
12344321
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();
}
}
}
Output
1 1
12 21
123321
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Spaced mirror (Examples 1–3)
O(n²)
O(1)
n rows, each printing 2n characters — still quadratic. Only loop counters are stored.
Remember
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.