Sequential Number Triangle (i + j - 1) in Java

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

What You’ll Learn

How to print a sequential number triangle in Java:

1, then 2 3, then 3 4 5, and so on.

You’ll generate each number with the simple formula i + j - 1, where i is the row and j is the column.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete Java Program

The outer loop controls the row count. The inner loop prints i values, and each value is computed as i + j - 1.

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

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print((i + j - 1) + " ");
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Set the row count

int rows = 5; controls the triangle height.

Setup
2

Outer loop controls rows

for (int i = 1; i <= rows; i++) increases the row length from 1 up to rows.

Row control
3

Inner loop prints i values

for (int j = 1; j <= i; j++) prints one value per column on the current row.

Printing
4

Compute the value as i + j - 1

On each row, the first value is i (because j=1). Then it increases by 1 as j moves across the row.

Value logic
=

Sequential i+j-1 triangle

Total numbers printed are 1+2+…+n = n(n+1)/2, so time complexity is 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 = 1; j <= i; j++) {
                System.out.print((i + j - 1) + " ");
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Avoid trailing spaces by printing a space only when j < i
  • Right-align the triangle by printing leading spaces before each row
  • Change the expression (for example i + j or i * j) to create new patterns
  • Use StringBuilder if you want custom separators (comma, dash, etc.)
  • Try printing odd numbers only by using 2 * (i + j - 1) - 1

Avoid

  • Hard-coding 5 instead of using rows
  • Mixing up row and column indices (keep i for rows, j for columns)
  • Forgetting the newline after each row
  • Closing System.in too early if you need more input later

Key Takeaways

1

Row i prints exactly i values.

2

The first value on each row is i (because i + 1 - 1 = i).

3

The formula i + j - 1 increases by 1 across the row.

4

Total printed values are n(n+1)/2, so runtime is O(n²).

❓ Frequently Asked Questions

Because the first column is j=1, so the formula becomes i + 1 - 1, which equals i.
Print a space only when j < i. That keeps spaces between values but not at the end of the row.
Yes. Add a constant offset: print (start - 1) + i + j - 1. For example, start at 10 with 9 + i + j - 1.
O(n²) for n rows because total prints are \(1+2+\dots+n\).

Explore More Java Number Patterns!

Try swapping the value formula to generate brand-new sequences with the same nested-loop structure.

All Number Patterns →
Did you know?

The expression i + j - 1 is a simple example of a diagonal pattern: numbers along each diagonal are the same, because they share the same i + j sum.

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