Number Mirror with Growing Stars in Java

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:
1234554321
1234**4321
123****321
12******21
1********1 Complete Java Program
Three inner loops: left ascending digits, middle stars, right descending digits.
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
Pick the size
rows = 5 controls the widest number and the number of rows printed.
Outer loop shrinks i
i runs from rows down to 1, so the number blocks shrink each row.
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.
Right numbers and newline
Finally, print i..1 to mirror the left side, then println() to move to the next row.
Mirrored rows
The star block grows by 2 characters per row while the number ranges shrink.
Variation — User Input Version
Let the user decide the row count at runtime using Scanner:
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
- Print a single
*instead of**for a different middle width - Insert spaces between digits to make the mirror more readable
- Replace stars with another character (like
#or-) - Validate input and ensure
rows > 0 - Compare with Program 1 which prints only numbers without a middle block
Avoid
- Forgetting the final descending-number loop (you lose the mirror)
- Printing stars outside the loop bounds (middle width won’t match)
- Using negative rows without validation
- Closing
System.inwithScannerif you still need input later in the same run
Key Takeaways
Three inner loops build one row: left numbers, middle stars, right numbers.
The star count is 2*(rows - i) when printing \"**\".
The number ranges shrink while the star range grows as i decreases.
Nested loops make it easy to compose mirrored patterns.
❓ Frequently Asked Questions
rows = 5 and i = 1, stars are printed 4 times as \"**\", so total star characters are 8.\"*\" in the star loop instead of \"**\". The middle block will grow by 1 per missing number instead of 2.Explore More Java Number Patterns!
Try mixing mirrors, padding, and different center blocks to create new console patterns.
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.
12 people found this page helpful
