A number & asterisk mirror prints ascending digits, a growing star center, then descending digits on each row — left and right halves mirror each other.
Remember
Rule: for i from n down to 1,
print 1..i, then (n - i) pairs of **, then i..1
1234554321
1234**4321
123****321
12******21
1********1 ← n = 5
Three inner loops do the work. Natural step after Program 22 (odd-length rows) and before centered pyramids.
Approach
How to Solve It
Descend i; print left digits, star pairs, then right digits.
Method
Idea
Best for
Three loops
1..i, then ** pairs, then i..1
Learning, interviews, exams
Custom fill
Same loops; print ## (or spaces) instead of **
When the center symbol should change
Pseudocode
Pseudocode
for i from n down to 1:
for j from 1 to i:
print j
for k from i to n - 1:
print "**"
for m from i down to 1:
print m
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (int i = n; i >= 1; i--)
Left half
for (int j = 1; j <= i; j++) System.out.print(j);
Star center
for (int k = i; k < n; k++) System.out.print("**");
Right half
for (int m = i; m >= 1; m--) System.out.print(m);
End the row
System.out.println();
Custom fill
Print "##" (or " ") instead of "**"
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit and each ** pair
System.out.println
Ends the current line
After all three inner loops
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the size n and the mirror pattern updates instantly.
Whole numbers from 1 to 9 (single digits keep the shape clean). Tap a chip or type a value.
Trace left digits, star pairs, and right digits for each row.
i
Left
Stars
Right
Printed row
4
1234
0
4321
12344321
3
123
1 × **
321
123**321
2
12
2 × **
21
12****21
1
1
3 × **
1
1******1
Star pairs per row: n - i. Every row has the same total width once digits and ** pairs are counted — that is why time is O(n²).
Code
Java Programs
Three complete programs: fixed n = 5, Scanner input, and a custom ## fill. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded size — left digits, ** center, right digits.
Java
public class NumberAsteriskMirror {
public static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++)
System.out.print(j);
for (int k = i; k < 5; k++)
System.out.print("**");
for (int m = i; m >= 1; m--)
System.out.print(m);
System.out.println();
}
}
}
1. Outer loop descends.i runs from n down to 1 — fewer digits each row.
2. Left then center. Print 1..i, then n - i pairs of **.
3. Mirror the right. Print i..1, then println to end the row.
When i = 5, there are no stars — full 1234554321. When i = 3, one less digit on each side and two ** pairs in the middle.
Example 2 — Size Input
Read n at runtime. Prefer hasNextInt() before nextInt() in real apps.
Java
import java.util.Scanner;
public class NumberAsteriskMirrorInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
System.out.print(j);
for (int k = i; k < n; k++)
System.out.print("**");
for (int m = i; m >= 1; m--)
System.out.print(m);
System.out.println();
}
sc.close();
}
}
Output (when user enters 3)
Enter n: 3
123321
12**21
1****1
How It Works
1. Prompt and read. Ask for a size, then store it with sc.nextInt().
2. Same three-loop core. Only the source of n changes — left, stars, and right stay identical to Example 1.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive whole number.");
return;
}
int n = sc.nextInt();
if (n < 1) {
System.out.println("Enter a positive whole number.");
return;
}
Example 3 — Custom Fill ##
Keep n = 5 but use hash pairs instead of asterisks in the center.
Java
public class NumberHashMirror {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++)
System.out.print(j);
for (int k = i; k < n; k++)
System.out.print("##");
for (int m = i; m >= 1; m--)
System.out.print(m);
System.out.println();
}
}
}
1. Same loops. Left 1..i and right i..1 are unchanged.
2. Different center. Only the fill string changes from "**" to "##".
3. Same shape. Row widths and digit halves stay identical — try spaces or other two-character fills the same way.
Edge Cases & Pitfalls
Check these before calling the solution done.
One *
Broken symmetry
Printing a single * per iteration grows the center by 1, not 2 — the halves no longer line up. Keep "**" (or another two-character fill).
Missing right
No mirror
Skipping the descending loop leaves only left digits and stars — not a mirror.
println inside
Broken rows
If println is inside any inner loop, the line splits early. Use print for digits and stars; println only after all three loops.
n = 1
Single 11
Output is 11 — left 1, no stars, right 1. A good sanity check.
n > 9
Multi-digit
Values like 10 print as two characters and skew the visual width. Keep n ≤ 9 for clean single-digit rows.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive whole number.
Analysis
Time and Space Complexity
Program
Time
Extra space
Three loops (Examples 1–3)
O(n²)
O(1)
Each of n rows prints O(n) characters (digits plus star pairs) — total work is quadratic in n.
Remember
Key Takeaways
Rule: for i from n down to 1, print 1..i, then ** pairs, then i..1.
Three loops: left digits, center fill, right digits — skip any one and the mirror breaks.
Break the row: call println only after all three inner loops.
Complexity:O(n²) time; O(1) extra space.
One line: print 1..i, grow the center with **, mirror with i..1, then println().
Frequently Asked Questions
Each iteration prints two asterisks so the center grows by 2 characters per row while keeping the pattern symmetric.
The pattern prints ascending numbers 1..i, then stars, then descending numbers i..1 on the same line.
Because i starts at n and decreases — each row prints fewer digits and more stars in the center.
System.out.print stays on the same line. System.out.println ends the current line. Digits and stars use print; the row break uses println after all three inner loops.
Three — ascending digits (j), star pairs (k), and descending digits (m).
Yes. Replace "**" with "##", two spaces, or any fill string. See Example 3.
O(n²) for size n because each of n rows prints O(n) characters overall.
Use sc.hasNextInt() before sc.nextInt(), require n ≥ 1, and reject non-numeric input — see Example 2 notes.
🤔
Did you know?
Each row prints 1..i, then a growing block of ** pairs, then i..1 — three inner loops create a symmetric mirror. As i shrinks, the star block grows to keep row width consistent.