Number Mirror with Growing Stars in Java

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a mirrored number pattern where the center gradually turns into stars:

1234554321, 1234**4321, 123****321, 12******21, 1********1.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1234554321
1234**4321
123****321
12******21
1********1
1

Complete Java Program

Three inner loops: left ascending digits, middle stars, right descending digits.

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

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

🧠 How It Works

1

Pick the size

rows = 5 controls the widest number and the number of rows printed.

Setup
2

Outer loop shrinks i

i runs from rows down to 1, so the number blocks shrink each row.

Rows
3

Left numbers + stars

First, print 1..i. Then print \"**\" for each missing value from i up to rows - 1, doubling the star count each row.

Middle
4

Right numbers and newline

Finally, print i..1 to mirror the left side, then println() to move to the next row.

Mirror
=

Mirrored rows

The star block grows by 2 characters per row while the number ranges shrink.

2

Variation — User Input Version

Let the user decide the row count at runtime using Scanner:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print(\"Enter the number of rows: \");
        int rows = sc.nextInt();

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

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

Key Takeaways

❓ Frequently Asked Questions

Explore More Java Number Patterns!

Try mixing mirrors, padding, and different center blocks to create new console patterns.

All Number Patterns →
Did you know?

The total width stays constant across rows because the missing digits are replaced by stars in the middle. That makes the output feel like a fixed-width “frame” that changes content.

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