Right-Aligned Increasing Number Triangle in Java

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

What You’ll Learn

How to print a right-aligned number triangle in Java where values increase continuously across rows:

1, then 2 3, then 4 5 6, up to 11 12 13 14 15.

You’ll use spacing plus System.out.printf("%3d", ...) to keep columns aligned.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete Java Program

Print blanks while j > i, otherwise print the next counter value k using fixed width formatting.

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 >= 1; j--) {
                if (j > i) {
                    System.out.print("   ");
                } else {
                    System.out.printf("%3d", k++);
                }
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Initialize the counter

int k = 1; is the next number to print.

Setup
2

Outer loop controls rows

i goes from 1 to rows, and each next row prints one more number than the previous.

Row control
3

Inner loop adds leading spaces

When j > i, we print " " to push the numbers to the right.

Alignment
4

Print the next number with %3d

System.out.printf("%3d", k++) prints a fixed width number and increments k.

Printing
=

Right-aligned increasing triangle

Total numbers printed are n(n+1)/2 for n rows (15 when n=5), so runtime grows like O(n²).

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 >= 1; j--) {
                if (j > i) {
                    System.out.print("   ");
                } else {
                    System.out.printf("%3d", k++);
                }
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Use %4d if you plan to print larger values (hundreds/thousands)
  • Center-align by printing leading spaces based on row index
  • Reset k on each row to print row-wise sequences instead of continuous numbering
  • Print separators like commas by building each row with StringBuilder
  • Convert it to a left-aligned triangle by removing the j > i spacing rule

Avoid

  • Printing raw numbers without fixed width once values reach 10+ (alignment will break)
  • Hard-coding 5 instead of using the rows variable
  • Forgetting the newline after each row
  • Closing System.in too early if more input is needed later

Key Takeaways

1

Leading blanks (" ") create the right alignment.

2

A counter k prints a continuous sequence across rows (1..15 for 5 rows).

3

printf("%3d", ...) keeps columns aligned for two-digit values.

4

Total printed numbers are n(n+1)/2, so runtime grows like O(n²).

❓ Frequently Asked Questions

For 5 rows, the total count is 1+2+3+4+5 = 15. Since k increments once per printed number, the last printed value is 15.
You already get separation because %3d includes leading spaces. If you want an explicit separator, you can print an extra space after each number.
Change rows. The loops and alignment will scale automatically. For larger outputs, consider changing %3d to %4d or more.
O(n²) for n rows because the nested loops perform work proportional to the printed triangle size.

Explore More Java Number Patterns!

Once alignment clicks, try combining spacing with mirrored loops to build pyramids and diamonds.

All Number Patterns →
Did you know?

Using fixed-width formatting like %3d is a simple way to keep console output readable when values grow beyond a single digit.

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