Right-Aligned Increasing Number Triangle in Java

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

What You’ll Learn

How to print a right-aligned triangle of increasing numbers in Java:

1, then 1 2, then 1 2 3, and so on up to rows.

You’ll use one loop for spaces (alignment) and another loop for numbers (1..i).

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete Java Program

First print spaces to shift the row right, then print the numbers from 1 to the current row index.

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

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

🧠 How It Works

1

Set the number of rows

rows = 5 decides how many lines are printed.

Setup
2

Outer loop controls the row length

On row i, you will print numbers from 1 to i.

Row control
3

Space loop right-aligns the row

The loop for (j = rows; j > i; j--) prints rows-i spaces before the numbers.

Alignment
4

Number loop prints 1..i

The loop for (k = 1; k <= i; k++) prints the increasing sequence for that row.

Numbers
=

Right-aligned number triangle

Total numbers printed are \(1+2+\dots+n = n(n+1)/2\), 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();

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

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Left-align it by removing the leading-space loop
  • Print the triangle as 1, 12, 123 by printing without spaces
  • Use StringBuilder if you want to build each row before printing
  • Turn it into a pyramid by printing an extra space between numbers and adjusting indentation
  • Print descending rows (reverse triangle) by looping i downward

Avoid

  • Hard-coding 5 throughout the code (use rows)
  • Printing inconsistent spaces (it will break alignment)
  • Forgetting System.out.println() after each row
  • Closing System.in with Scanner if you need input later in the same JVM run

Key Takeaways

1

Print spaces first to align the triangle to the right.

2

Then print numbers from 1 to the row index i.

3

Total printed numbers are n(n+1)/2.

4

The same approach applies to many aligned patterns: indent + content.

❓ Frequently Asked Questions

Spaces create indentation. When indentation decreases each row, the triangle becomes right-aligned.
%2d prints each number in a 2-character field, keeping spacing consistent and the output visually neat.
Replace System.out.format("%2d", k) with System.out.print(k). You may also need to adjust indentation for alignment.
O(n²) because total prints are \(1+2+\dots+n\).

Explore More Java Number Patterns!

Alignment patterns are a great way to level up your nested-loop skills.

All Number Patterns →
Did you know?

Right-aligned patterns are often made by printing spaces first. The same trick applies to right-aligned stars, pyramids, and numeric staircases.

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