Java Descending Number Triangle Pattern (Left-Aligned)
Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A left-aligned descending number triangle keeps the same starting digit on every row while the tail gets shorter — from rows..1 down to a single rows.
Remember
Rule: for i from 1 to rows,
print j from rows down to i
54321
5432
543
54
5 ← rows = 5
Unlike Program 3 (first digit changes each row), here every row starts at rows.
Approach
How to Solve It
Walk i from 1 to rows; on each row print j from rows down to i.
Method
Idea
Best for
Nested loops
Outer i = 1..rows; inner j = rows..i descending
Learning, interviews, exams
Spaced digits
Same loops; print each value with a trailing space
Readable output when rows > 9
Pseudocode
Pseudocode
for i from 1 to rows:
for j from rows down to i:
print j
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (int i = 1; i <= rows; i++)
Descending digits
for (int j = rows; j >= i; j--)
Print digit
System.out.print(j);
End the row
System.out.println();
Digits per row
rows - i + 1
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit in the inner loop
System.out.println
Ends the current line
After the inner loop finishes
Print values without a newline, then end the row once.
Try it
Live Preview
Change the row count and the 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
5432
543
54
5
Trace
Worked Walkthrough — rows = 5
Trace how j always starts at 5 and stops at i.
i
Range j
Count
Printed row
1
5..1
5
54321
2
5..2
4
5432
3
5..3
3
543
4
5..4
2
54
5
5
1
5
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 — outer ascending, inner descending from rows.
Java
public class LeftAlignedDescendingTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--)
System.out.print(j);
System.out.println();
}
}
}
Output
54321
5432
543
54
5
How It Works
1. Outer loop.i is the lowest digit printed on that row.
2. Inner loop.j always starts at rows and counts down to i.
3. End the row.println() runs only after the inner loop finishes.
When i = 1: 5..1 → 54321. When i = 3: 5..3 → 543.
Example 2 — Rows Input
Read rows at runtime. Same nested-loop core.
Java
import java.util.Scanner;
public class LeftAlignedDescendingTriangleInput {
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 = rows; j >= i; j--)
System.out.print(j);
System.out.println();
}
sc.close();
}
}
Output (when user enters 5)
Enter the number of rows: 5
54321
5432
543
54
5
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 Output
Same nested loops — each digit printed with a trailing space for readability.
Java
public class LeftAlignedDescendingTriangleSpaced {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= i; j--)
System.out.print(j + " ");
System.out.println();
}
}
}
Output
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
How It Works
1. Same structure. Loops unchanged — only the print adds a space.
2. Why space? Digits stay readable when values reach two digits.
3. Width tip. For larger rows, prefer format("%2d ", j) for aligned columns.
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.
Wrong start
Looks like Program 3
Starting j at rows - i + 1 instead of rows changes the first digit each row.
Ascending j
Reversed digits
Using j++ from i to rows prints ascending rows like Program 5, not this pattern.
No println
One long line
Forgetting println() after the row glues every digit onto one endless line.
rows = 1
Single digit
Output is just 1 on one line.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Descending triangle (Examples 1–3)
O(n²)
O(1)
Total digits = n(n+1)/2 — still quadratic. Only loop counters are stored.
Remember
Key Takeaways
Rule: row i prints j from rows down to i.
Fixed prefix: every row starts at rows; only the tail shortens.
Break the row: call println only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for each row, print j from rows down to i with print, then println().
Frequently Asked Questions
Because the inner loop always begins at j = rows. Only the stopping point changes as i grows, so each row starts with the maximum number.
The outer loop runs i from 1 to rows. For each row, the inner loop runs j from rows down to i and prints j, then println ends the row.
Program 3 prints 54321, then 4321, then 321 (the first digit changes). Program 4 prints 54321, then 5432, then 543 (the first digit stays at rows).
Yes. Print System.out.print(j + " ") in the inner loop — see Example 3.
O(n²) for n rows. Total printed numbers are n + (n-1) + … + 1 = n(n+1)/2.
Program 5 prints ascending rows 1, 12, 123. Program 4 prints descending digits from rows down to i on each row.
Use sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
The inner loop always starts at rows and counts down to i, so every row begins with the same digit while the tail shortens — 54321, 5432, 543, and so on.