A reverse descending number triangle shrinks each row while printing digits from i down to 1 — so the top row is longest and the bottom row is a single 1.
Remember
Rule: for i from rows down to 1,
print j from i down to 1
54321
4321
321
21
1 ← rows = 5
Unlike Program 2 (left-shifted i..rows), here both loops count downward and each row ends at 1.
Approach
How to Solve It
Descend i from rows to 1; for each row, print j from i down to 1.
Method
Idea
Best for
Descending nested loops
Outer i = rows..1; inner j = i..1
Learning, interviews, exams
Spaced digits
Same loops; print each digit with a trailing space
When wide rows need gaps
Pseudocode
Pseudocode
for i from rows down to 1:
for j from i down to 1:
print j
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (int i = rows; i >= 1; i--)
Print digits
for (int j = i; j >= 1; j--) System.out.print(j);
End the row
System.out.println();
Spaced digits
System.out.print(j + " ");
Ascending variant
Keep outer; change inner to j = 1..i
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit on the current row
System.out.println
Ends the current line
After the inner loop finishes
Print digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the reverse descending triangle updates instantly.
Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultrows = 5 · 15 digits
54321
4321
321
21
1
Trace
Worked Walkthrough — rows = 5
Trace i, the inner range j = i..1, and the printed row.
i
Inner j
Digits
Printed row
5
5..1
5
54321
4
4..1
4
4321
3
3..1
3
321
2
2..1
2
21
1
1..1
1
1
Total digits = 5 + 4 + 3 + 2 + 1 = 15 — the triangular number n(n+1)/2.
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 — both loops count downward.
Java
public class ReverseDescendingTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = i; j >= 1; j--)
System.out.print(j);
System.out.println();
}
}
}
Output
54321
4321
321
21
1
How It Works
1. Outer loop shrinks.i runs from 5 down to 1 — each step shortens the row.
2. Inner loop descends. Print j from i down to 1 with print.
3. Break the row. Call println once after the inner loop.
When i = 5: 54321. When i = 1: a single 1.
Example 2 — Rows Input
Read rows at runtime. Same nested descending loops.
Java
import java.util.Scanner;
public class ReverseDescendingTriangleInput {
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 = rows; i >= 1; i--) {
for (int j = i; j >= 1; j--)
System.out.print(j);
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
4321
321
21
1
How It Works
1. Prompt and guard. Read rows; exit early if it is less than 1.
2. Same core. Only the source of rows changes from a literal to user input.
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
Keep rows = 5 but print each digit followed by a space.
Java
public class ReverseDescendingTriangleSpaced {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = i; j >= 1; j--)
System.out.print(j + " ");
System.out.println();
}
}
}
Output
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
How It Works
1. Same structure. Outer and inner bounds match Example 1 exactly.
2. Only the print changes.j + " " adds a trailing space after each digit.
3. Same order. Digit sequence is unchanged — only spacing differs.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of digits
If println is inside the inner loop, each digit lands on its own line. Use print for digits; println only after the inner loop.
Ascending outer
Growing triangle
Using i = 1..rows with j = i..1 grows the triangle upward instead of shrinking from the top.
Wrong inner start
Off-by-one rows
Starting j at rows instead of i prints the full descending line every time.
rows = 1
Single digit
Output is just 1 — the smallest non-empty case.
rows ≤ 0
Empty output
The outer loop never runs — print nothing or show a message.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Reverse descending (Examples 1–3)
O(n²)
O(1)
Total digits = n + (n−1) + … + 1 = n(n+1)/2 — still quadratic. Only loop counters are stored.
Remember
Key Takeaways
Rule: for i from rows down to 1, print j from i down to 1.
Both loops descend: outer shrinks the row; inner prints reverse digits.
Break the row: call println only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for i = rows..1, print j = i..1, then println().
Frequently Asked Questions
The inner loop runs j from i down to 1, so each row prints i, i-1, …, 1.
Because rows = 5 and the outer loop starts at i = rows. The first row prints from 5 down to 1.
Program 2 prints i..rows (left-shifted ascending digits). Program 3 prints i..1 (reverse descending) with a shrinking outer loop.
Keep the descending outer loop but change the inner loop to j = 1..i (ascending).
Replace 5 with rows in the outer bound — see Example 2.
Use System.out.print(j + " ") instead of System.out.print(j) — see Example 3.
O(n²) for n rows. Total prints are n + (n-1) + … + 1 = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.
🤔
Did you know?
This pattern prints each row in descending order from i down to 1. The outer loop shrinks the row length while the inner loop counts downward — producing 54321, 4321, 321, and so on.