Shape Rule
i..2 + suffix
Row i: print j = i..2, then k = 1..(rows+1-i).

The decreasing-increasing pattern prints 12345, 21234, 32123, 43212, 54321 — each row combines a descending prefix and ascending suffix. This tutorial covers dual inner-loop logic, live preview, worked Java examples, edge cases, and O(n²) complexity.
i..2 + suffix
Row i: print j = i..2, then k = 1..(rows+1-i).
Two per row
Decreasing loop first, increasing loop second — then row break.
rows digits
Every row prints exactly rows digits — pivot shifts each line.
Suffix only
When i=1, decreasing loop skips — output is 12345.
3–12 rows
Pick a row count and draw the decreasing-increasing pattern instantly in the browser.
Complexity
Each row prints rows digits — total work grows as n².
A decreasing-increasing number pattern builds row i with a descending prefix (i..2) and an ascending suffix (1..(rows+1-i)). Every row has exactly rows digits.
In Java you use nested loops: outer for (i = 1; i <= rows; i++), inner for (j = i; j > 1; j--) print j, inner for (k = 1; k <= rows+1-i; k++) print k, then println() after both inner loops.
It is a dual inner-loop exercise that connects descending and ascending segments on each row.
i = 1..rows picks each row number.
j = i..2, k = 1..suffix prints decreasing + increasing each row.
Decrease first, increase second — two inner loops per row.
Follow Program 49 multiplication triangle; continue to Program 51 alternating triangle.
In short: outer i=1..rows, inner j=i..2 print j, inner k=1..(rows+1-i) print k, then println().
Given rows = 5, print five lines: 12345, 21234, 32123, 43212, 54321.
// rows = 5 (conceptual output)
// 12345
// 21234
// 32123
// 43212
// 54321 | Item | Type | Description |
|---|---|---|
rows | int | How many lines to print (typically ≥ 1). |
i, j | int | Row index i; decreasing loop j and increasing loop k. |
| Printed output | text | Exactly rows digits per row — fixed width. |
for i from 1 to rows:
for j from i down to 2: print j
for k from 1 to (rows+1-i): print k
newline | Approach | Idea | Best for |
|---|---|---|
| Dual inner loops | print(j) then print(k) in two inner loops | Fixed row width — complementary loop bounds |
| Scanner input | sc.nextInt() for rows | User-chosen row count |
| Spaced digits | Print space after each digit in both loops | Easier reading for larger rows — Example 3 |
| Goal | Pattern |
|---|---|
| Set rows | int rows = 5; |
| Outer loop | for (i = 1; i <= rows; i++) |
| Decrease + increase | for (j = i; j > 1; j--) and for (k = 1; k <= rows+1-i; k++) |
| Print digit | System.out.print(j) and System.out.print(k) |
| Row break | System.out.println(); after both inner loops |
| Program 49 contrast | Multiplication triangle uses i×j products; this pattern uses dual inner loops per row |
How outer row selection, decreasing prefix, increasing suffix, and row breaks work together.
for (i = 1; i <= rows; i++)Picks row number i — pattern height.
for (j = i; j > 1; j--)
for (k = 1; k <= rows+1-i; k++)Prints exactly rows digits on row i.
print(j); print(k);Decreasing digits first, then increasing digits — no multiplication.
trace i=3Dry-run row 3: decreasing 32 + increasing 123 → 32123.
Reach for this pattern when teaching dual inner loops, complementary bounds, and fixed-width row output.
Classic follow-up after multiplication triangle patterns like Program 49.
Outer/inner bound practice with an immediate visual check.
Combine loops with Scanner for a flexible row count.
Compare with Program 49 (multiplication triangle), then continue to Program 51 (alternating triangle).
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in complementary loop bounds, pivot shifting, and O(n²) thinking.
Choose a row count and draw the decreasing-increasing 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 two inner loops per row — decrease then increase.
rows = 5Hard-coded size — first inner loop prints i..2, second prints 1..(rows+1-i).
public class DecreasingIncreasingNumberPattern {
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 k = 1; k <= (rows + 1 - i); k++) {
System.out.print(k);
}
System.out.println();
}
}
} When i = 3, decreasing prints 32, increasing prints 123 — output 32123. Row 1 skips the decreasing loop and prints 12345.
Read rows with Scanner for flexible output size.
Same dual inner loops; row count comes from user input.
import java.util.Scanner;
public class DecreasingIncreasingNumberPatternInput {
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 k = 1; k <= (rows + 1 - i); k++) {
System.out.print(k);
}
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 DecreasingIncreasingNumberPatternSpaced {
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 k = 1; k <= (rows + 1 - i); k++) {
System.out.print(k + " ");
}
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.
for (j = i; j > 1; j--) print j, then for (k = 1; k <= rows+1-i; k++) print k — building each row.
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 with rows = 5 to see how both inner loops build 32123.
| Phase | Loop | Row so far |
|---|---|---|
| Decrease | j=3 → print 3; j=2 → print 2 | 32 |
| Increase | k=1,2,3 → print 1,2,3 | 32123 |
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 shifts the pivot between decreasing and increasing segments.
Example: compare row 3 (32123) — decreasing 32 plus increasing 123.
Practice println vs print for multi-line vs single-line output.
Example: use print(k + " ") 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.
Fixed row width with shifting pivot makes O(n²) concrete for beginners.
Example: count digits for rows=5 → 5 rows × 5 digits = 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.
Fixed row width makes bound mistakes obvious — each line should have exactly rows digits.
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 decreasing loop first, then increasing loop, then println().
Use print(j) and print(k) in the inner loops; one println() per row after both loops.
Trace rows = 5, i = 3 on paper — expect 32123.
Pro Tip: if row lengths vary, check whether decreasing and increasing bounds are complementary.
Mistakes that commonly break decreasing-increasing number patterns.
Each digit lands on its own line — you get a column, not a fixed-width row.
→ Use print inside both inner loops; println() only after they finish.
Mismatched decreasing/increasing bounds change row length — lines no longer have exactly rows digits.
→ Keep for (j = i; j > 1; j--) and for (k = 1; k <= rows+1-i; k++) for fixed width.
Omitting println() after both inner loops glues all rows onto one line.
→ Always call System.out.println() after both inner loops complete.
Breaking between decreasing and increasing loops splits one row across two lines.
→ Run both inner loops back-to-back, then call println() once per row.
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 (decreasing loop skips; suffix prints one digit).
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 digits — fine for labs; use smaller n for quick demos.
Unchecked Scanner leaves rows unset — call sc.hasNextInt() first.
Add spaces after each digit in both inner loops — see Example 3.
Try these variations to lock in the pattern.
rows = 3, 6, or 8rows digitsj >= 1 instead of j > 1for (i = rows; i >= 1; i--)rows² (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 digit.j > 1; increasing uses k <= rows+1-i — bounds must complement for fixed width.Quick Takeaway: outer i=1..rows, inner j=i..2 print j, inner k=1..(rows+1-i) print k, 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 decreasing-increasing pattern combines dual inner loops per row — a natural step after multiplication 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 51 for the alternating ascending/descending triangle pattern.
Keep println() after both inner loops — one row break per outer iteration.
i, inner j=i..2, and inner k=1..(rows+1-i) before codingprint(j) and print(k), then println() after both inner loopsrows ≥ 1 for interactive programsScanner return value before using rowsprintln() between the two inner loops (breaks row shape)j > 1 for decreasing and k <= rows+1-i for increasingrowsrows = 1 edge casePrint the pattern the beginner-friendly way.
Each row prints rows digits
i = 1..rows
Codej = i..2, k = 1..suffix
Logicn digits/row
O(n²)
AnalysisEach row combines a decreasing prefix (i..2) and an increasing suffix (1..(rows+1-i)). Row 1 prints only the suffix — 12345; row 3 gives 32123.
Move on to the alternating ascending/descending triangle in the Java number-pattern series.
12 people found this page helpful