Java Number Mirror Pattern (Asterisk Center)

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

What Is This Pattern?

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.

How to Solve It

Descend i; print left digits, star pairs, then right digits.

MethodIdeaBest for
Three loops1..i, then ** pairs, then i..1Learning, interviews, exams
Custom fillSame 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

GoalPattern
Walk rowsfor (int i = n; i >= 1; i--)
Left halffor (int j = 1; j <= i; j++) System.out.print(j);
Star centerfor (int k = i; k < n; k++) System.out.print("**");
Right halffor (int m = i; m >= 1; m--) System.out.print(m);
End the rowSystem.out.println();
Custom fillPrint "##" (or " ") instead of "**"

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit and each ** pair
System.out.printlnEnds the current lineAfter all three inner loops

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

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.

Live result n = 5 · 5 rows
1234554321
1234**4321
123****321
12******21
1********1

Worked Walkthrough — n = 4

Trace left digits, star pairs, and right digits for each row.

iLeftStarsRightPrinted row
412340432112344321
31231 × **321123**321
2122 × **2112****21
113 × **11******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²).

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

How It Works

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

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

How It Works

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.

Time and Space Complexity

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

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.

Next: Centered Continuous Pyramid

Move on to the centered continuous number pyramid in the Java number-pattern series.

Program 24 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