Number Triangle Pattern Using i + j in Java

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

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:

Output
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
1

Complete Java Program

Loop i from 0 to rows. On each row, loop j from 0 to i and print i + j.

Java
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

1

Set the size

rows = 5 controls the last row index (0..5), so this prints 6 lines.

Setup
2

Outer loop (rows from 0)

for (int i = 0; i <= rows; i++) moves from row 0 to row 5, increasing row length each time.

Row control
3

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.

Printing
4

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.

Value logic
=

i+j triangle from 0

The total values printed are \(1+2+\dots+(rows+1)\), so runtime grows like O(n²).

2

Variation — User Input Version

Let the user choose the last row index 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 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 + 1 while keeping loops from 0
  • Right-align the triangle by printing leading spaces before each row
  • Replace i + j with i * j to generate a multiplication triangle
  • Use StringBuilder for 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

1

The outer loop starts at 0, so the first row prints a single value: 0.

2

Row i prints i+1 values because j runs from 0 to i.

3

Each number is computed using i + j, which increases sequentially across the row.

4

Total prints scale quadratically, so time complexity is O(n²).

❓ Frequently Asked Questions

The last row has i=5 and the last column has j=5, so the last value is i + j = 5 + 5 = 10.
If you loop i from 0 to rows (inclusive), it prints rows + 1 lines.
Yes. Keep loops from 0 but print i + j + 1, or start loops from 1 and print i + j - 1.
O(n²) because the program prints a growing number of values each row.

Explore More Java Number Patterns!

Try changing the start index (0 vs 1) or the value formula to create entirely new triangles.

All Number Patterns →
Did you know?

Patterns that use i + j often create consistent diagonals because every step right increases j by 1 and therefore increases the value by 1.

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