Java Palindrome Number Pattern (Increasing-Decreasing)
Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
An increasing-decreasing palindrome pattern starts each row at i, prints an increasing run, then mirrors downward — without repeating the peak digit.
Remember
Rule: for i = 1..rows
m = i
print m++ (i times)
m = m - 2
print m-- (i-1 times)
1
232
34543
4567654
567898765 ← rows = 5
Unlike Program 51 (running counter with odd/even direction), here each row is self-contained: climb from i, then descend with m = m - 2.
Approach
How to Solve It
Start m = i, print increasing, adjust with m = m - 2, then print decreasing.
Method
Idea
Best for
Fixed rows
Hard-code rows; dual inner loops
Labs and demos
Scanner input
Same loops; read rows at runtime
Interactive practice
Spaced digits
print(m + " ") in both loops
Easier-to-read rows
Pseudocode
Pseudocode
for i from 1 to rows:
m = i
for j from 1 to i:
print m; m = m + 1
m = m - 2
for k from 1 to i - 1:
print m; m = m - 1
new line
Cheat sheet
Goal
Pattern
Start row
int m = i;
Increase
for (int j = 1; j <= i; j++) print(m++);
Skip peak
m = m - 2;
Decrease
for (int k = 1; k < i; k++) print(m--);
End row
System.out.println();
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit in both inner loops
System.out.println
Ends the line
After both inner loops finish
Glue all digits with print, then break once per row.
Try it
Live Preview
Change the row count and the palindrome rows update instantly (kept to single digits).
Whole numbers from 3 to 5 (keeps peak digits single). Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · digits = 25
1
232
34543
4567654
567898765
Trace
Worked Walkthrough — rows = 5
Trace the increase half, the m = m - 2 step, and the decrease half.
i
Increase
After m−2
Decrease
Printed row
1
1
(skip)
(none)
1
2
23
2
2
232
3
345
4
43
34543
4
4567
6
654
4567654
5
56789
8
8765
567898765
After printing 345, m is 6; m = m - 2 lands on 4 so the peak 5 is not printed twice.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input, and spaced digits. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded size — start m = i, increase, m = m - 2, then decrease.
Java
public class IncreasingDecreasingPalindrome {
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();
}
}
}
Output
1
232
34543
4567654
567898765
How It Works
1. Start at i. Each row begins with m = i — row 3 starts at 3.
2. Climb then adjust. Print m++ for i digits; then m = m - 2 so the peak is not duplicated.
3. Descend. Print m-- for i - 1 digits — row 3 becomes 34543.
Example 2 — Scanner Input
Read rows at runtime. Same dual-loop logic.
Java
import java.util.Scanner;
public class IncreasingDecreasingPalindromeInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
if (rows < 1) return;
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();
}
}
Output (when user enters 5)
Enter the number of rows: 5
1
232
34543
4567654
567898765
How It Works
1. Prompt and guard. Read rows; exit early if it is less than 1.
2. Same loops. Only the source of rows changes — the increase / m−2 / decrease structure is identical.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Spaced Digits
Same dual-loop logic — a space after each digit for clearer rows.
Java
public class IncreasingDecreasingPalindromeSpaced {
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();
}
}
}
Output
1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
How It Works
1. Same math. Still increase, m = m - 2, then decrease.
2. Different format.print(m + " ") separates digits so the mirror shape is easier to see.
3. Same width. Row i still has 2i - 1 values — only spacing changes.
Edge Cases & Pitfalls
Check these before calling the solution done.
Forgot m−2
Doubled peak
Without m = m - 2, row 3 prints 345543 instead of 34543.
k <= i
Extra digit
The decrease loop must be k < i (i−1 times). Using k <= i adds one value too many.
m = 1
Wrong start
Each row must start at m = i, not 1 — otherwise every row begins the same way.
rows = 1
Single digit
Output is just 1 — the decrease loop does not run.
rows > 5
Multi-digit values
Without spaces, values like 10 merge into the string. Prefer spaced output or keep rows ≤ 5 for single digits.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
All three examples
O(n²)
O(1)
Row i prints 2i − 1 digits; total for n rows is n².
Remember
Key Takeaways
Rule: start at i, increase i times, m = m - 2, decrease i−1 times.
Center:m = m - 2 prevents duplicating the peak digit.
Width: row i has 2i − 1 digits.
Complexity:O(n²) prints for n rows.
One line: climb from i, skip the peak with m−2, then mirror down.
Frequently Asked Questions
Row 3 prints increasing 345, then after m = m - 2 the decreasing part prints 43 — combined: 34543.
After the increasing loop, m is one past the last printed value. m = m - 2 moves back to the previous digit so the peak is not duplicated.
The decreasing half has i-1 digits — one less than the increasing half to avoid repeating the center.
Yes. Use a variable rows in the outer loop — the same two-loop structure works for any positive n.
Print a space after each digit in both inner loops — see Example 3.
Row i prints 2i - 1 digits — a palindrome-like width that grows each line.
O(n²) for n rows. Row i prints 2i-1 digits; total work grows quadratically.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
🤔
Did you know?
Each row starts from i, prints an increasing run of length i, then a decreasing run of length i-1. The key step is m = m - 2 so the peak digit is not duplicated — row 3 gives 34543.