Right-Aligned Increasing Number Triangle in Java

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:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5Complete Java Program
First print spaces to shift the row right, then print the numbers from 1 to the current row index.
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
Set the number of rows
rows = 5 decides how many lines are printed.
Outer loop controls the row length
On row i, you will print numbers from 1 to i.
Space loop right-aligns the row
The loop for (j = rows; j > i; j--) prints rows-i spaces before the numbers.
Number loop prints 1..i
The loop for (k = 1; k <= i; k++) prints the increasing sequence for that row.
Right-aligned number triangle
Total numbers printed are \(1+2+\dots+n = n(n+1)/2\), so runtime grows like O(n²).
Variation — User Input Version
Let the user choose the number of rows using Scanner:
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,123by printing without spaces - Use
StringBuilderif 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
idownward
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.inwithScannerif you need input later in the same JVM run
Key Takeaways
Print spaces first to align the triangle to the right.
Then print numbers from 1 to the row index i.
Total printed numbers are n(n+1)/2.
The same approach applies to many aligned patterns: indent + content.
❓ Frequently Asked Questions
%2d prints each number in a 2-character field, keeping spacing consistent and the output visually neat.System.out.format("%2d", k) with System.out.print(k). You may also need to adjust indentation for alignment.Explore More Java Number Patterns!
Alignment patterns are a great way to level up your nested-loop skills.
Right-aligned patterns are often made by printing spaces first. The same trick applies to right-aligned stars, pyramids, and numeric staircases.
12 people found this page helpful
