Mirror Number Pattern in Java

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

What You’ll Learn

How to print the mirror number pattern in Java:

1, then 212, then 32123, up to 543212345.

Each row prints a decreasing sequence from i down to 2, then an increasing sequence from 1 up to i.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
212
32123
4321234
543212345
1

Complete Java Program

Use two inner loops: one prints i..2 (descending), the other prints 1..i (ascending).

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

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

🧠 How It Works

1

Choose the number of rows

rows = 5 prints 5 lines of the mirror pattern.

Setup
2

Outer loop controls the row number

i goes from 1 to rows, and each row gets longer.

Row control
3

Print the left half (descending)

for (j = i; j > 1; j--) prints i i-1 ... 2.

Left half
4

Print the right half (ascending)

for (j = 1; j <= i; j++) prints 1 2 ... i, creating the mirror effect.

Right half
=

Mirror rows

Each row prints 2i-1 digits, and the total work grows like O(n²).

2

Variation — User Input Version

Let the user choose the row count 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 = 1; i <= rows; i++) {
            for (int j = i; j > 1; j--) {
                System.out.print(j);
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits (System.out.print(j + " ")) for readability
  • Center-align the rows by printing leading spaces before the descending part
  • Build each row using StringBuilder for more complex formatting
  • Print the pattern upside-down by reversing the outer loop
  • Replace numbers with letters to create an alphabet mirror pattern

Avoid

  • Printing down to 1 in the first loop (it duplicates the center 1)
  • Hard-coding 5 instead of using a rows variable
  • Forgetting the newline after each row
  • Closing System.in too early if you need more input later

Key Takeaways

1

Each row combines a descending and an ascending loop.

2

The descending loop stops at 2 so the center 1 appears only once.

3

Row i prints 2i - 1 digits.

4

Total work grows like O(n²) for n rows.

❓ Frequently Asked Questions

This version prints digits without separators to match the classic output like 32123. You can add spaces by printing j + " " in both loops.
For row i, the length is 2i-1. For i=5, that’s 9 digits: 543212345.
Yes. Print rows - i leading spaces (or space blocks) before the descending loop to center each row.
O(n²) because each row prints \(2i-1\) digits and i ranges from 1..n.

Explore More Java Number Patterns!

Mirror patterns are great practice for combining multiple loops into a single row.

All Number Patterns →
Did you know?

Rows like 32123 are a simple form of a palindrome. Many palindrome patterns are built by printing a descending part and then an ascending part.

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