Right-Aligned Descending Number Triangle in Java

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
        5
      5 4
    5 4 3
  5 4 3 2
5 4 3 2 1
1

Complete Java Program

Print two-space blocks for right alignment, then print numbers from rows down to the current row value i.

Java
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

1

Set the maximum value

rows = 5 defines both the maximum printed number and the number of rows.

Setup
2

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.

Row control
3

First inner loop prints leading spaces

j < i prints i-1 blocks of " " so the triangle becomes right-aligned.

Alignment
4

Second inner loop prints descending numbers

for (int j = rows; j >= i; j--) prints 5 4 3 ... until it reaches the current row value.

Printing
=

Right-aligned descending triangle

Total values printed are n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user choose the size using Scanner:

Java
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..rows instead of rows..i
  • Center-align by printing more leading spaces per row
  • Build each line with StringBuilder for 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.in too early if you need more input later

Key Takeaways

1

Leading spaces make the triangle right-aligned.

2

Each row prints from rows down to the current row value.

3

The number of printed values grows as n(n+1)/2.

4

This pattern combines two classic ideas: alignment + descending loops.

❓ Frequently Asked Questions

On the first iteration i = rows, so the number loop runs from rows down to i and prints only one value: 5.
Change the number-printing loop to count up: for (int j = i; j <= rows; j++).
Yes. Remove the first inner loop that prints spaces, and just print the descending numbers.
O(n²) for n rows because total prints are \(1+2+\dots+n\).

Explore More Java Number Patterns!

Combine alignment with number direction (ascending/descending) to unlock a wide range of triangles and pyramids.

All Number Patterns →
Did you know?

Right alignment is often just a rule like “print spaces until a threshold”. Once you master that, you can build many triangles and diamonds.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful