Shape Rule
m++ / m--
Row i: increase i digits, then decrease i-1 digits.

The palindrome row pattern prints 1, 232, 34543, 4567654, 567898765 — each row increases then mirrors downward. This tutorial covers dual inner-loop logic, the m = m - 2 center step, live preview, worked Java examples, edge cases, and O(n²) complexity.
m++ / m--
Row i: increase i digits, then decrease i-1 digits.
m = i
Each row begins at its row number — row 4 starts from 4.
m = m - 2
Between loops, step back so the peak digit is not printed twice.
2i - 1 digits
Row i has 2i-1 digits — width grows each line.
3–12 rows
Pick a row count and draw the palindrome row pattern instantly in the browser.
Complexity
Total digits ≈ n² — work grows quadratically.
An increasing-decreasing palindrome row pattern starts each row at i, prints an increasing run, then mirrors downward. Row 3 gives 34543; row 4 gives 4567654.
In Java: outer for (i = 1; i <= rows; i++), m = i, inner increase loop print(m++), m = m - 2, inner decrease loop print(m--), then println() after both inner loops.
It is a dual inner-loop exercise that builds palindrome-like rows with increase then decrease segments.
i = 1..rows picks each row number.
First loop prints m++; after m = m - 2, second loop prints m--.
Each row resets m = i — increase loop then decrease loop per row.
Follow Program 51 alternating triangle; continue to Program 53 diagonal mirror.
In short: outer i=1..rows, m=i, inner j=1..i print m++, m=m-2, inner k=1..i-1 print m--, then println().
Given rows = 5, print five lines: 1, 232, 34543, 4567654, 567898765.
// rows = 5 (conceptual output)
// 1
// 232
// 34543
// 4567654
// 567898765 | Item | Type | Description |
|---|---|---|
rows | int | How many lines to print (typically ≥ 1). |
i, j | int | Row index i; variable m with increase loop and decrease loop. |
| Printed output | text | 2i-1 digits on row i — palindrome-like width. |
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
newline | Approach | Idea | Best for |
|---|---|---|
| Dual inner loops | print(m++) then print(m--) with m = m - 2 between | Palindrome width — 2i-1 digits per row |
| Scanner input | sc.nextInt() for rows | User-chosen row count |
| Spaced digits | Print space after each digit in both loops | Easier reading for wider rows — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | int rows = 5; |
| Outer loop | for (i = 1; i <= rows; i++) |
| Row start | int m = i; |
| Increase loop | for (j = 1; j <= i; j++) print(m++); |
| Center step | m = m - 2; |
| Decrease loop | for (k = 1; k < i; k++) print(m--); |
| Row break | System.out.println(); after both inner loops |
| Program 51 contrast | Alternating triangle uses running counter k; this pattern uses dual inner loops with m = m - 2 |
How outer row selection, increase loop, center step, and decrease loop work together.
for (i = 1; i <= rows; i++)Picks row number i — also the starting digit.
m = i
for (j = 1; j <= i; j++)
print(m++)Prints i ascending digits on row i.
m = m - 2
for (k = 1; k < i; k++)
print(m--)Prints i-1 descending digits — mirrors without duplicating peak.
trace i=3Dry-run row 3: increasing 345 + decreasing 43 → 34543.
Reach for this pattern when teaching dual inner loops, palindrome rows, and the m = m - 2 center step.
Classic follow-up after alternating triangle patterns like Program 51.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 51 (alternating triangle), then continue to Program 53 (diagonal mirror).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in dual inner loops, m = m - 2, and O(n²) thinking.
Choose a row count and draw the palindrome row pattern in the browser.
Three complete Java programs — fixed rows = 5, Scanner input, and a spaced-digit variant. Click View Output to reveal sample console results.
Print five rows with increase then decrease loops — palindrome-like rows.
rows = 5Hard-coded size — start m = i, increase loop, m = m - 2, then decrease loop.
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();
}
}
} When i = 3, increasing prints 345, then m = m - 2 gives decreasing 43 — output 34543. Row 1 has only the increasing half — output is 1.
Read rows with Scanner for flexible output size.
Same dual inner loops; row count comes from user input.
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();
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();
}
} Identical loop structure to Example 1; only the row count is dynamic.
Add spaces between digits for easier reading.
Print a space after each digit in both inner loops.
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();
}
}
} Same dual-loop logic; only the output format adds spaces between digits.
System.out is built in; use Scanner when reading input. Set rows (e.g. 5).
for (i = 1; i <= rows; i++) — one iteration per output line.
First loop prints m++ for i steps; after m = m - 2, second loop prints m-- for i-1 steps.
System.out.println(); after both inner loops moves to the next row.
Total prints ≈ n² digits — O(n²) time, O(1) extra memory.
i = 3Trace row 3 to see how increase, m = m - 2, and decrease build 34543.
| Phase | m | Row so far |
|---|---|---|
| Start | 3 | — |
| Increase j=1,2,3 | 6 | 345 |
| m = m - 2 | 4 | 345 |
| Decrease k=1 | 3 | 3454 |
| Decrease k=2 | 2 | 34543 |
After both inner loops, println() moves to the next row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: use Scanner for dynamic row count — see Example 2.
Each row mirrors after the peak — m = m - 2 prevents duplicating the center digit.
Example: compare row 3 (34543) — increasing 345 plus decreasing 43.
Practice println vs print for multi-line vs single-line output.
Example: use print(m++ + " ") in both loops for spaced output — Example 3.
Add spaces after each digit in both inner loops for readability.
Example: print rows=5 with spaced output and compare readability.
Palindrome width 2i-1 makes O(n²) concrete for beginners.
Example: count digits for rows=5 → 1+3+5+7+9 = 25 prints.
Pair the pattern with Scanner and positive-row checks.
Example: reject rows <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner loop roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner Java courses.
Missing m = m - 2 shows immediately — center digit appears twice in each row.
Only loops and console output — no arrays or math libraries.
Change rows, use Scanner, or add spaces between digits in both inner loops.
Streaming output needs no storage beyond loop counters.
Pro Tip: learn the dual inner loops first; then try Scanner input and the spaced-digit variant in Example 3.
Small habits that keep number-pattern code clean.
Use rows for height and i/j for row/column indices.
ScannerAvoid crashes when the user types letters instead of a number.
Run increase loop, apply m = m - 2, run decrease loop, then println().
Use print(m++) and print(m--) in the inner loops; one println() per row after both loops.
Trace rows = 5, i = 3 on paper — expect 34543.
Pro Tip: if rows have duplicated center digits, check whether you forgot m = m - 2.
Mistakes that commonly break increasing-decreasing palindrome row patterns.
Each digit lands on its own line — you get a column, not a palindrome row.
→ Use print inside the inner loop; println() only after it finishes.
Without m = m - 2, the peak digit prints twice — row 3 becomes 345543 instead of 34543.
→ Always apply m = m - 2 between the increase and decrease loops.
Omitting println() after the inner loop glues all rows onto one line.
→ Always call System.out.println() after the inner loop completes.
Using k <= i in the decrease loop duplicates the peak digit.
→ Use k < i for the decrease loop — exactly i-1 descending digits.
Letters or empty input throw InputMismatchException.
→ Use sc.hasNextInt() before sc.nextInt().
Using literal 10 in loop bounds instead of variable rows breaks dynamic input.
→ Use one rows variable for the outer loop bound.
Check these inputs before calling the solution done.
Output is one line: 1.
Loop never runs — print nothing or show a message.
rows < 0Treat as invalid; re-prompt instead of silent empty output.
Large row counts produce many values — fine for labs; use smaller n for quick demos.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
Use conditional spacing to avoid trailing spaces — see Example 3.
Try these variations to lock in the pattern.
rows = 3, 6, or 82i-1 digits and mirrors after the peakm = i * i instead of m = in² (e.g. 25 digits for rows=5).print stays on the line; println advances — mix them carefully.rows > 0 for interactive programs; rows = 1 prints one value.print(m++); after m = m - 2, decrease loop uses print(m--) with k < i.Quick Takeaway: outer i=1..rows, m=i, inner j=1..i print m++, m=m-2, inner k=1..i-1 print m--, then println().
| Program | Time | Extra space |
|---|---|---|
| Fixed rows = 5 (Example 1) | O(n²) | O(1) |
| Scanner input (Example 2) | O(n²) | O(1) |
| Spaced digits (Example 3) | O(n²) | O(1) |
The palindrome row pattern combines dual inner loops with m = m - 2 — a natural step after alternating triangle patterns. Master the fixed-rows version first, then try Scanner input and the spaced-digit variant in Example 3.
Practice the three examples above, then continue to Program 53 for the diagonal mirror number pattern.
Keep println() after both inner loops — one row break per outer iteration.
i, m = i, increase loop, m = m - 2, and decrease loop before codingprint(m++) and print(m--), then println() after both inner loopsrows ≥ 1 for interactive programsScanner return value before using rowsk <= i in decrease loop (duplicates peak)m = m - 2 between the two inner loopsrowsrows = 1 edge casePrint the pattern the beginner-friendly way.
Each row prints 2i-1 digits
i = 1..rows
CodeUp: print m++; down: print m--
Logic2i-1 digits/row
O(n²)
AnalysisEach 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.
Move on to the diagonal mirror number pattern in the Java number-pattern series.
12 people found this page helpful