Increasing-Decreasing Number Pattern in Java

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:
1
232
34543
4567654
567898765 Complete Java Program
Two loops per row: one prints increasing values, and the other prints decreasing values to form a palindrome-like row.
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
Pick the row number
The outer loop runs i = 1..rows. Row i defines the starting digit.
Increase for i steps
Start m = i and print it i times while incrementing. For i=4 this prints 4567.
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.
Decrease for i-1 steps
Print m while decrementing for i-1 steps. For i=4, this prints 654 and completes 4567654.
Palindrome-like rows
Each row prints about 2i-1 digits, so overall printing grows like O(n²).
Variation — User Input Version
Let the user choose the number of rows 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 = 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
StringBuilderto 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
rowsas a variable - Mixing up loop bounds (
k < iis important for the second half) - Closing
System.inwithScannerif you still need console input elsewhere in the same JVM run
Key Takeaways
Row i starts from i and prints increasing values first.
The second half prints decreasing values to mirror the row.
m = m - 2 avoids repeating the highest value.
Total printing grows quadratically, so complexity is O(n²).
❓ Frequently Asked Questions
23 and then prints the decreasing value 2, making 232.i prints i digits on the way up and i-1 digits on the way down, totaling 2i-1.Explore More Java Number Patterns!
Mirror-style patterns are a fun way to practice loop boundaries and symmetry.
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.
12 people found this page helpful
