Java Descending Number Triangle Pattern (Right-Aligned)
Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview
Definition
What Is This Pattern?
A right-aligned descending number triangle pads each row with leading spaces, then prints digits from i down to 1 — so the triangle grows toward the right edge.
Remember
Rule: for i from 1 to rows,
for j from rows down to 1:
print space if j > i else print j
1
21
321
4321
54321 ← rows = 5
Unlike Program 3 (left-aligned reverse descending, no padding), here a fixed-width inner loop fills unused columns with spaces.
Approach
How to Solve It
Walk i from 1 to rows; in one fixed-width loop, print a space when j > i, otherwise print j.
Method
Idea
Best for
Fixed-width if/else
Space if j > i; else digit j
Learning, interviews, exams
Ternary form
Same logic with j > i ? " " : j
Shorter code once the idea clicks
Pseudocode
Pseudocode
for i from 1 to rows:
for j from rows down to 1:
print (" " if j > i else j)
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (int i = 1; i <= rows; i++)
Fixed-width column
for (int j = rows; j >= 1; j--)
Space or digit
if (j > i) System.out.print(" "); else System.out.print(j);
End the row
System.out.println();
Ternary
System.out.print(j > i ? " " : j);
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each space or digit
System.out.println
Ends the current line
After the inner loop finishes
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the row count and the right-aligned 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 · width 5
1
21
321
4321
54321
Trace
Worked Walkthrough — rows = 5
Trace leading spaces (rows − i) and the descending digit group for each i.
i
Spaces
Digits
Printed row
1
4
1
1
2
3
21
21
3
2
321
321
4
1
4321
4321
5
0
54321
54321
Each row prints exactly rows characters — total work is O(n²) for n rows.
Code
Java Programs
Three complete programs: fixed rows = 5, Scanner input with ternary form, and a compact dry-run. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded width — if/else in one fixed-width descending loop.
Java
public class RightAlignedDescending {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 5; j >= 1; j--) {
if (j > i)
System.out.print(" ");
else
System.out.print(j);
}
System.out.println();
}
}
}
Output
1
21
321
4321
54321
How It Works
1. Outer loop grows.i runs from 1 to 5 — one right-aligned row per iteration.
2. Inner loop is fixed-width.j always runs from 5 down to 1.
3. Space or digit. Print a space while j > i; otherwise print j.
When i = 3: two spaces + 321. When i = 5: no spaces — 54321.
Example 2 — Rows Input
Read rows at runtime. Ternary replaces if/else; inner loop uses rows as width.
Java
import java.util.Scanner;
public class RightAlignedInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter rows: ");
int rows = sc.nextInt();
if (rows < 1) return;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= 1; j--)
System.out.print(j > i ? " " : j);
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter rows: 4
1
21
321
4321
How It Works
1. Prompt and guard. Read rows; exit early if it is less than 1.
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 — Compact rows = 3
Same if/else structure as Example 1 — smaller size for paper tracing.
Java
public class RightAlignedSmall {
public static void main(String[] args) {
int rows = 3;
for (int i = 1; i <= rows; i++) {
for (int j = rows; j >= 1; j--) {
if (j > i) System.out.print(" ");
else System.out.print(j);
}
System.out.println();
}
}
}
Output
1
21
321
How It Works
1. Same structure. Fixed-width descending loop — only rows changes from 5 to 3.
2. Spaces shrink. Row 1 has 2 leading spaces; row 2 has 1; row 3 has none.
3. Dry-run first. Trace i = 1..3 on paper before coding the full rows = 5 demo.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of digits
If println is inside the inner loop, each character lands on its own line. Use print for spaces/digits; println only after the inner loop.
Skip spaces
Left-aligned
Omitting the space branch collapses the pattern into Program 3’s left-aligned triangle.
Wrong test
Inverted padding
Using j < i for spaces (instead of j > i) pads the wrong side and breaks the descending digits.
rows = 1
Single digit
Output is just 1 — no leading spaces.
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
Right-aligned descending (Examples 1–3)
O(n²)
O(1)
n rows, each printing n characters — quadratic. Only loop counters are stored.
Remember
Key Takeaways
Rule: for each i, loop j = rows..1 — space if j > i, else print j.
Fixed width: the inner loop always runs rows times so columns stay aligned.
Break the row: call println only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: for j = rows..1, print space if j > i else j, then println().
Frequently Asked Questions
Leading spaces are printed while j > i. Smaller rows get more spaces, pushing digits to the right edge.
The inner loop runs j from rows down to 1. When j <= i, it prints j — naturally producing i..1 on each row.
Running j from rows down to 1 on every row keeps column alignment. Spaces fill positions where j > i.
Program 3 prints a left-aligned reverse descending triangle with no leading spaces. Program 30 pads with spaces for right alignment.
Replace 5 with rows in the inner loop bound — see Example 2.
O(n²) for n rows because each row runs a fixed-width inner loop of n iterations.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.
Yes — System.out.print(j > i ? " " : j) compacts the if/else logic.
🤔
Did you know?
This pattern uses a fixed column width (rows). For each row i, the inner loop prints spaces while j > i, then prints digits in descending order — producing a right-aligned triangle.