Increasing-Decreasing Number Pattern in Java

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

What You’ll Learn

How to print the pattern 1, 232, 34543, 4567654, 567898765 in Java.

Each row first increases from i, then decreases back to i without repeating the center value.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
232
34543
4567654
567898765
1

Complete Java Program

Two loops per row: one prints increasing values, and the other prints decreasing values to form a palindrome-like row.

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

        for (int i = 1; i <= rows; i++) {
            int m = i;

            for (int j = 1; j <= i; j++) {
                System.out.print(m++);
            }

            m = m - 2;
            for (int k = 1; k < i; k++) {
                System.out.print(m--);
            }

            System.out.println();
        }
    }
}

🧠 How It Works

1

Pick the row number

The outer loop runs i = 1..rows. Row i defines the starting digit.

Row
2

Increase for i steps

Start m = i and print it i times while incrementing. For i=4 this prints 4567.

Up
3

Step back to avoid duplication

After the first loop, m is one past the last printed number. Using m = m - 2 moves it back to the previous number so the peak isn’t repeated.

Center
4

Decrease for i-1 steps

Print m while decrementing for i-1 steps. For i=4, this prints 654 and completes 4567654.

Down
=

Palindrome-like rows

Each row prints about 2i-1 digits, so overall printing grows like O(n²).

2

Variation — User Input Version

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

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Add spaces between digits for readability when rows grow
  • Start each row from a different base number (e.g., m = i*i)
  • Right-align rows by printing leading spaces
  • Use StringBuilder to build each row before printing
  • Try printing only the increasing half to make a different pattern

Avoid

  • Forgetting m = m - 2 (it prevents duplicating the peak)
  • Hard-coding 5 everywhere; use rows as a variable
  • Mixing up loop bounds (k < i is important for the second half)
  • Closing System.in with Scanner if you still need console input elsewhere in the same JVM run

Key Takeaways

1

Row i starts from i and prints increasing values first.

2

The second half prints decreasing values to mirror the row.

3

m = m - 2 avoids repeating the highest value.

4

Total printing grows quadratically, so complexity is O(n²).

❓ Frequently Asked Questions

Row 2 prints increasing values 23 and then prints the decreasing value 2, making 232.
Row i prints i digits on the way up and i-1 digits on the way down, totaling 2i-1.
Yes. Print a space after each number and keep both loops unchanged. The symmetry remains the same.
O(n²) for n rows because the output size grows on the order of \(n^2\).

Explore More Java Number Patterns!

Mirror-style patterns are a fun way to practice loop boundaries and symmetry.

All Number Patterns →
Did you know?

Many mirror patterns can be generated by printing an increasing part and then printing the same sequence in reverse—just be careful to avoid repeating the middle value.

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