Right-Aligned Increasing Number Triangle in Java

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:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15Complete Java Program
Print blanks while j > i, otherwise print the next counter value k using fixed width formatting.
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
Initialize the counter
int k = 1; is the next number to print.
Outer loop controls rows
i goes from 1 to rows, and each next row prints one more number than the previous.
Inner loop adds leading spaces
When j > i, we print " " to push the numbers to the right.
Print the next number with %3d
System.out.printf("%3d", k++) prints a fixed width number and increments k.
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²).
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();
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
%4dif you plan to print larger values (hundreds/thousands) - Center-align by printing leading spaces based on row index
- Reset
kon 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 > ispacing rule
Avoid
- Printing raw numbers without fixed width once values reach 10+ (alignment will break)
- Hard-coding 5 instead of using the
rowsvariable - Forgetting the newline after each row
- Closing
System.intoo early if more input is needed later
Key Takeaways
Leading blanks (" ") create the right alignment.
A counter k prints a continuous sequence across rows (1..15 for 5 rows).
printf("%3d", ...) keeps columns aligned for two-digit values.
Total printed numbers are n(n+1)/2, so runtime grows like O(n²).
❓ Frequently Asked Questions
1+2+3+4+5 = 15. Since k increments once per printed number, the last printed value is 15.%3d includes leading spaces. If you want an explicit separator, you can print an extra space after each number.rows. The loops and alignment will scale automatically. For larger outputs, consider changing %3d to %4d or more.Explore More Java Number Patterns!
Once alignment clicks, try combining spacing with mirrored loops to build pyramids and diamonds.
Using fixed-width formatting like %3d is a simple way to keep console output readable when values grow beyond a single digit.
12 people found this page helpful
