Number Triangle Pattern Using i + j in Java

What You’ll Learn
How to print a number triangle pattern in Java where row i starts at i and increases sequentially across the row:
0, then 1 2, then 2 3 4, and so on.
You’ll build this using nested loops that start from 0 and print the value i + j.
⭐ Pattern Output
For rows = 5 (outer loop runs from 0 to 5), the pattern looks like this:
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10Complete Java Program
Loop i from 0 to rows. On each row, loop j from 0 to i and print i + j.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i <= rows; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((i + j) + " ");
}
System.out.println();
}
}
}🧠 How It Works
Set the size
rows = 5 controls the last row index (0..5), so this prints 6 lines.
Outer loop (rows from 0)
for (int i = 0; i <= rows; i++) moves from row 0 to row 5, increasing row length each time.
Inner loop prints i+1 values
for (int j = 0; j <= i; j++) runs i+1 times, so the triangle grows by one number per row.
Value is computed as i + j
The first value on each row is i (when j=0), and it increases by 1 as j increments.
i+j triangle from 0
The total values printed are \(1+2+\dots+(rows+1)\), so runtime grows like O(n²).
Variation — User Input Version
Let the user choose the last row index 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 last row index (e.g., 5): ");
int rows = sc.nextInt();
for (int i = 0; i <= rows; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((i + j) + " ");
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Avoid trailing spaces by printing a space only when
j < i - Start from 1 by printing
i + j + 1while keeping loops from 0 - Right-align the triangle by printing leading spaces before each row
- Replace
i + jwithi * jto generate a multiplication triangle - Use
StringBuilderfor advanced formatting and alignment
Avoid
- Confusing row count with last index (0..rows prints rows+1 lines)
- Hard-coding 5 instead of using
rows - Mixing up row and column indices
- Forgetting the newline after each row
Key Takeaways
The outer loop starts at 0, so the first row prints a single value: 0.
Row i prints i+1 values because j runs from 0 to i.
Each number is computed using i + j, which increases sequentially across the row.
Total prints scale quadratically, so time complexity is O(n²).
❓ Frequently Asked Questions
i=5 and the last column has j=5, so the last value is i + j = 5 + 5 = 10.i from 0 to rows (inclusive), it prints rows + 1 lines.i + j + 1, or start loops from 1 and print i + j - 1.Explore More Java Number Patterns!
Try changing the start index (0 vs 1) or the value formula to create entirely new triangles.
Patterns that use i + j often create consistent diagonals because every step right increases j by 1 and therefore increases the value by 1.
12 people found this page helpful
