A decreasing-increasing number pattern builds each row from two parts: a descending prefix (i..2) and an ascending suffix (1..(rows+1−i)). Every row has exactly rows digits.
Remember
Rule: for i = 1..rows
print j from i down to 2
print k from 1 to (rows+1-i)
12345
21234
32123
43212
54321 ← rows = 5
Unlike Program 49 (products i×j), here two complementary loops shift a pivot across a fixed-width row.
Approach
How to Solve It
For each row i, run a decreasing loop first, then an increasing loop — then break the line.
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(digit + " ") in both loops
Easier-to-read rows
Pseudocode
Pseudocode
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
new line
Cheat sheet
Goal
Pattern
Set size
int rows = 5;
Walk rows
for (int i = 1; i <= rows; i++)
Decreasing part
for (int j = i; j > 1; j--)
Increasing part
for (int k = 1; k <= rows + 1 - i; k++)
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 decreasing-increasing pattern updates instantly.
Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · digits = 25
12345
21234
32123
43212
54321
Trace
Worked Walkthrough — rows = 5
Trace how the decreasing prefix and increasing suffix combine on each row.
i
Decreasing
Increasing
Printed row
1
(none)
1..5
12345
2
2
1..4
21234
3
32
1..3
32123
4
432
1..2
43212
5
5432
1
54321
Row 1 skips the decreasing loop (j = 1; j > 1 is false) — only the suffix prints.
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 — decreasing loop, then increasing loop, then a line break.
Java
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();
}
}
}
Output
12345
21234
32123
43212
54321
How It Works
1. Outer loop.i is the row number and the start of the decreasing prefix.
2. Two inner loops. First print j from i down to 2; then print k from 1 to rows+1−i.
3. Fixed width. Together both loops always print exactly rows digits before println().
Example 2 — Scanner Input
Read rows at runtime. Same dual-loop logic.
Java
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();
if (rows < 1) return;
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();
}
}
Output (when user enters 5)
Enter the number of rows: 5
12345
21234
32123
43212
54321
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 dual-loop 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 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();
}
}
}
Output
1 2 3 4 5
2 1 2 3 4
3 2 1 2 3
4 3 2 1 2
5 4 3 2 1
How It Works
1. Same bounds. Still print decreasing j then increasing k.
2. Different format.print(digit + " ") separates values so the pivot is easier to see.
3. Same shape. Each row still has exactly rows numbers — only spacing changes.
Edge Cases & Pitfalls
Check these before calling the solution done.
Skip decreasing
Wrong first row
Using j >= 1 instead of j > 1 adds an extra 1 before the suffix on every row.
Wrong bound
Uneven rows
The increasing limit must be rows + 1 - i. Using i or rows alone breaks the fixed width.
Missing println
One long line
Without println() after both loops, every digit prints on a single horizontal line.
rows = 1
Single digit
Output is just 1.
rows = 0
Empty output
The outer loop never runs. Guard interactive input with rows >= 1.
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)
Each of n rows prints exactly n digits → n² prints total.
Remember
Key Takeaways
Rule: decreasing i..2, then increasing 1..(rows+1−i).
Width: every row has exactly rows digits — the pivot shifts.
Spacing: use print for digits and one println after both loops.
Complexity:O(n²) prints for n rows.
One line: for each i, print down from i to 2, then up from 1 to rows+1−i.
Frequently Asked Questions
Row i prints j from i down to 2, then k from 1 up to (rows+1-i). Concatenating both parts creates lines like 21234 and 32123.
For i=1, the decreasing loop (j=i..2) does not run, so only the increasing loop prints 1..rows.
For i=3, decreasing prints 32, increasing prints 123 (rows+1-i=3), giving 32123.
Yes. Use a variable rows — the same two-loop structure works for any positive n.
Print a space after each digit in both inner loops — see Example 3.
Yes. Each row prints exactly rows digits — the pivot shifts each line.
O(n²) for n rows. Each row prints O(n) digits.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
🤔
Did you know?
Each 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.