Java Number Triangle Pattern (Consecutive Offset)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An increasing number triangle from 1 prints row i with i values computed as i + j − 1 — so each row starts at the row number and counts upward.

Remember
Rule: for i from 1 to rows,
      for j from 1 to i:
        print (i + j - 1) + " "

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9     ← rows = 5

Unlike Program 32 (formula 9 + i + j, starts at 11), here there is no base offset — the first cell is always 1.

How to Solve It

Grow i from 1 to rows; for each row print j = 1..i values of i + j − 1.

MethodIdeaBest for
Formula i + j − 1Nested loops; row i starts at iLearning, interviews, exams
User-input rowsSame formula; read height with ScannerWhen triangle size must vary

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print (i + j - 1) + " "
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (int i = 1; i <= rows; i++)
Print i valuesfor (int j = 1; j <= i; j++)
Compute valueSystem.out.print((i + j - 1) + " ");
End the rowSystem.out.println();
Row start checkWhen j = 1, value equals i

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach number plus trailing space
System.out.printlnEnds the current lineAfter the inner loop finishes

Print values without a newline, then end the row once.

Live Preview

Change the row count and the increasing triangle updates instantly.

Whole numbers from 3 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 15 values
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9

Worked Walkthrough — rows = 5

Trace i, the inner range j = 1..i, and i + j − 1 for each cell.

iValues of i + j − 1Printed row
111
22, 32 3
33, 4, 53 4 5
44 … 74 5 6 7
55 … 95 6 7 8 9

Total values = 1 + 2 + … + 5 = 15 — the triangular number n(n+1)/2.

Java Programs

Three complete programs: fixed rows = 5, Scanner input, and a compact dry-run. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded size — formula i + j − 1 with a trailing space.

Java
public class IncreasingIjMinus1 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++)
                System.out.print((i + j - 1) + " ");
            System.out.println();
        }
    }
}

How It Works

1. Outer loop grows. i runs from 1 to 5 — one longer row each time.

2. Inner loop prints i values. For each j from 1 to i, print i + j − 1 plus a space.

3. Break the row. Call println once after the inner loop.

When i = 1: 1+1−1 = 1. When i = 3: 3 4 5.

Example 2 — Rows Input

Read rows at runtime. Same nested loops and formula.

Java
import java.util.Scanner;

public class IncreasingIjMinus1Input {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter rows: ");
        int rows = sc.nextInt();
        if (rows < 1) return;

        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();
    }
}

How It Works

1. Prompt and guard. Read rows; exit early if it is less than 1.

2. Same formula. Only the source of rows changes from a literal to user input.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive integer.");
    return;
}
int rows = sc.nextInt();
if (rows < 1) {
    System.out.println("Enter a positive integer.");
    return;
}

Example 3 — Compact rows = 3

Same nested-loop formula as Example 1 — smaller size for paper tracing.

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

        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. Same structure. Nested loops and i + j − 1 — only rows changes from 5 to 3.

2. Three rows. 1, then 2 3, then 3 4 5.

3. Dry-run first. Trace i = 1..3 on paper before coding the full rows = 5 demo.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Column of numbers

If println is inside the inner loop, each value lands on its own line. Use print for values; println only after the inner loop.

Wrong formula

Shifted start

Using i + j or 9 + i + j shifts every value — rows no longer start at the row number.

Wrong inner bound

Rectangle

j <= rows prints a rectangle — every row has the same width.

Missing space

Glued digits

Omitting + " " concatenates numbers like 23 instead of 2 3.

rows = 1

Single value

Output is just 1.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
Increasing from 1 (Examples 1–3)O(n²)O(1)

Total values = n(n+1)/2 — still quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: for each i, print j = 1..i values of i + j − 1.
  • Row start: when j = 1, the value equals i.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: for i = 1..rows, print (i + j − 1) + " " for j = 1..i, then println().

Frequently Asked Questions

Because when j = 1, the expression i + j - 1 becomes i. Row 4 therefore starts with 4.
j increases by 1, so i + j - 1 increases by 1 as well — producing consecutive numbers on each row.
Program 32 uses 9 + i + j (starts at 11). Program 33 uses i + j - 1 (starts at 1).
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
System.out.print((i + j - 1) + " ") keeps values separated on the same row. System.out.println() ends the row.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + … + n = n(n+1)/2.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.

Did you know?

Each printed value is computed as i + j - 1. Row i = 1 prints 1; row i = 4 prints 4, 5, 6, 7 — a left-shifted increasing triangle starting at 1.

Next: Increasing Triangle from 0

Move on to the increasing triangle starting from 0 in the Java number-pattern series.

Program 34 tutorial →

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