Right-Aligned Descending Number Triangle in Java

What You’ll Learn
How to print a right-aligned descending number triangle in Java:
5, then 5 4, then 5 4 3, down to 5 4 3 2 1.
You’ll use one loop for leading spaces and another loop to print descending numbers from 5 to the current row value.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1Complete Java Program
Print two-space blocks for right alignment, then print numbers from rows down to the current row value i.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int j = rows; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
}🧠 How It Works
Set the maximum value
rows = 5 defines both the maximum printed number and the number of rows.
Outer loop counts down
for (int i = rows; i >= 1; i--) starts at 5 and moves down to 1, growing the printed number list each line.
First inner loop prints leading spaces
j < i prints i-1 blocks of " " so the triangle becomes right-aligned.
Second inner loop prints descending numbers
for (int j = rows; j >= i; j--) prints 5 4 3 ... until it reaches the current row value.
Right-aligned descending triangle
Total values printed are n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user choose the size using Scanner:
import java.util.Scanner;
public class Main {
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 = rows; i >= 1; i--) {
for (int j = 1; j < i; j++) {
System.out.print(" ");
}
for (int j = rows; j >= i; j--) {
System.out.print(j + " ");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Print two spaces after each number (
j + " ") for a wider look - Use
printf("%2d ", j)for consistent spacing when values can be > 9 - Reverse the number direction to print
i..rowsinstead ofrows..i - Center-align by printing more leading spaces per row
- Build each line with
StringBuilderfor custom formatting
Avoid
- Hard-coding 5 everywhere instead of using
rows - Printing the numbers without a separator (it becomes hard to read)
- Forgetting the newline after each row
- Closing
System.intoo early if you need more input later
Key Takeaways
Leading spaces make the triangle right-aligned.
Each row prints from rows down to the current row value.
The number of printed values grows as n(n+1)/2.
This pattern combines two classic ideas: alignment + descending loops.
❓ Frequently Asked Questions
i = rows, so the number loop runs from rows down to i and prints only one value: 5.for (int j = i; j <= rows; j++).Explore More Java Number Patterns!
Combine alignment with number direction (ascending/descending) to unlock a wide range of triangles and pyramids.
Right alignment is often just a rule like “print spaces until a threshold”. Once you master that, you can build many triangles and diamonds.
12 people found this page helpful
