Sequential Decreasing-Width Number Triangle in Java

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

What You’ll Learn

How to print consecutive numbers in a triangle where each next row prints one fewer value.

We use System.out.format("%3d", value) to keep columns aligned.

⭐ Pattern Output

For rows = 5, the output looks like this:

Output
  1  2  3  4  5\n  6  7  8  9\n 10 11 12\n 13 14\n 15
1

Complete Java Program

The outer loop controls rows, and the inner loop prints a decreasing count while k keeps increasing.

Java
public class Main {
    public static void main(String[] args) {
        int rows = 5;
        int k = 1;

        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= i; j--) {
                System.out.format("%3d", k++);
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Initialize rows and k

rows sets the triangle height. k starts at 1 and increments after each print.

Setup
2

Outer loop prints each row

for (int i = 1; i <= rows; i++) moves from top row to bottom row.

Row control
3

Inner loop decreases the width

for (int j = rows; j >= i; j--) runs fewer times as i increases, so each next row is shorter.

Printing
4

Format output into columns

%3d keeps numbers aligned even when they reach two digits.

Formatting
=

Sequential triangle

The loop prints rows + (rows-1) + ... + 1 numbers, which is O(n²) overall.

2

Variation — User Input Version

Let the user choose the number of rows 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();

        int k = 1;
        for (int i = 1; i <= rows; i++) {
            for (int j = rows; j >= i; j--) {
                System.out.format("%3d", k++);
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Start at a different number by changing k (e.g., int k = 10;)
  • Increase the field width for larger values (e.g., %4d)
  • Build rows with StringBuilder for more complex formatting
  • Flip the triangle by changing the inner loop range

Avoid

  • Hard-coding 5 everywhere instead of using rows
  • Forgetting System.out.println() after each row
  • Using System.out.print without spacing (columns can misalign for 2+ digits)
  • Closing System.in if you still need console input elsewhere in the same JVM run

Key Takeaways

1

k stores the next number to print and increments after every output.

2

The inner loop count decreases as i grows, so each next row is shorter.

3

System.out.format("%3d", value) keeps the grid aligned.

4

Total prints are \(n(n+1)/2\), so runtime scales like O(n²).

❓ Frequently Asked Questions

Use a wider format like %4d or print an extra space after each value.
Yes. Start i from rows down to 1 and adjust the inner loop accordingly.
Formatting gives consistent column widths so the triangle stays readable when numbers grow.
Increase the field width (%4d, %5d, etc.) to keep alignment as values get larger.

Explore More Java Number Patterns!

Try changing the starting value and formatting width to create new sequential patterns.

All Number Patterns →
Did you know?

You can compute total printed values in this triangle as \(1 + 2 + \dots + rows = rows(rows+1)/2\).

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