Descending Number Triangle in Java

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

What You’ll Learn

How to print a descending number triangle in Java using nested for loops. Each row starts at 1 and prints up to a decreasing limit: rows, rows-1, ..., down to 1.

This pattern is a simple way to understand how the outer loop controls rows and the inner loop controls how many numbers appear per row.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
12345
1234
123
12
1
1

Complete Java Program

The outer loop starts from rows and counts down. The inner loop prints numbers from 1 to the current row limit.

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(j);
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Choose the row count

int rows = 5; sets how many lines will be printed.

Setup
2

Outer loop (descending rows)

for (int i = rows; i >= 1; i--) controls the triangle height and ensures each next row has one fewer number than the previous.

Row control
3

Inner loop (print 1..i)

for (int j = 1; j <= i; j++) prints digits from 1 up to the current row limit i using System.out.print(j).

Number printing
4

New line

System.out.println(); moves to the next row after each line.

Line break
=

Descending number triangle

Total numbers printed: 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the number of rows at runtime 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(j);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Validate input (check Scanner.hasNextInt()) before using rows
  • Print the ascending triangle by counting the outer loop up
  • Add spaces between numbers with System.out.print(j + " ")
  • Right-align the triangle by printing leading spaces before each row
  • Replace numbers with characters to create alphabet patterns

Avoid

  • Forgetting System.out.println() between rows
  • Using negative or zero rows without validating input
  • Mixing row/column logic (keep the outer loop for rows)
  • Closing System.in with Scanner if you still need console input elsewhere in the same JVM run

Key Takeaways

1

The outer loop decreases the row length (from rows down to 1).

2

The inner loop prints numbers from 1 to the current row limit i.

3

Total printed digits follow the triangular number count: n(n+1)/2.

4

The same nested-loop idea applies to star triangles and alphabet triangles.

❓ Frequently Asked Questions

Because each next row prints one fewer number than the previous: 12345, then 1234, then 123, and so on until 1.
The outer loop sets the row limit i (from rows down to 1). The inner loop prints j from 1 to i, then calls System.out.println().
Use for (int i = 1; i <= rows; i++) as the outer loop. Each row will then grow from 1 number up to rows numbers.
O(n²) for n rows: total prints are 1+2+…+n = n(n+1)/2.

Explore More Java Number Patterns!

From pyramids to Floyd’s triangle and beyond—practice nested loops with progressively richer patterns.

All Number Patterns →
Did you know?

The total number of printed digits grows like a triangular number. If you print n rows, the total count is 1+2+…+n = n(n+1)/2. For n = 5, that’s 15 digits.

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