Java Number Triangle Pattern (Starting from 0)

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

What Is This Pattern?

An increasing number triangle from 0 uses zero-based loops: row i prints i + 1 values of i + j — so the first cell is 0 and each row starts at i.

Remember
Rule: for i from 0 to max,
      for j from 0 to i:
        print (i + j) + " "

0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10     ← max = 5

Unlike Program 33 (formula i + j − 1, loops from 1), here both loops start at 0 and the formula is simply i + j.

How to Solve It

Walk i from 0 to max; for each row print j = 0..i values of i + j.

MethodIdeaBest for
Formula i + jZero-based nested loops; row i starts at iLearning, interviews, exams
User-input maxSame formula; read upper bound with ScannerWhen triangle size must vary

Pseudocode

Pseudocode
for i from 0 to max:
    for j from 0 to i:
        print (i + j) + " "
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (int i = 0; i <= max; i++)
Print i+1 valuesfor (int j = 0; j <= i; j++)
Compute valueSystem.out.print((i + j) + " ");
End the rowSystem.out.println();
Row start checkWhen j = 0, 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 max i value and the zero-based triangle updates instantly.

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

Live result max = 5 · 6 rows
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10

Worked Walkthrough — max = 5

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

iValues of i + jPrinted row
000
11, 21 2
22, 3, 42 3 4
33 … 63 4 5 6
44 … 84 5 6 7 8
55 … 105 6 7 8 9 10

Total values = 1 + 2 + … + 6 = 21 — formula (max+1)(max+2)/2.

Java Programs

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

Example 1 — Fixed max = 5

Hard-coded bound — formula i + j with zero-based nested loops.

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

How It Works

1. Outer loop from zero. i runs from 0 to 5 — six rows total.

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

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

When i = 0: 0+0 = 0. When i = 2: 2 3 4.

Example 2 — Max Input

Read max at runtime. Same nested loops and formula.

Java
import java.util.Scanner;

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

        for (int i = 0; i <= max; i++) {
            for (int j = 0; j <= i; j++)
                System.out.print((i + j) + " ");
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and guard. Read max; exit early if it is negative.

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

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a non-negative integer.");
    return;
}
int max = sc.nextInt();
if (max < 0) {
    System.out.println("Enter a non-negative integer.");
    return;
}

Example 3 — Compact max = 2

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

Java
public class IncreasingFrom0Small {
    public static void main(String[] args) {
        int max = 2;

        for (int i = 0; i <= max; i++) {
            for (int j = 0; j <= i; j++)
                System.out.print((i + j) + " ");
            System.out.println();
        }
    }
}

How It Works

1. Same structure. Zero-based loops and i + j — only max changes from 5 to 2.

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

3. Dry-run first. Trace i = 0..2 on paper before coding the full max = 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.

Start at 1

Missing 0

Starting i at 1 (or using i + j − 1) shifts the triangle — it no longer starts at 0.

Wrong inner bound

Rectangle

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

Missing space

Glued digits

Omitting + " " concatenates numbers like 12 instead of 1 2.

max = 0

Single value

Output is just 0.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a non-negative integer.

Time and Space Complexity

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

With i = 0..max, total values = (max+1)(max+2)/2 — still quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: for each i from 0 to max, print j = 0..i values of i + j.
  • Row start: when j = 0, 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 = 0..max, print (i + j) + " " for j = 0..i, then println().

Frequently Asked Questions

Because the loops start at i = 0 and j = 0, so i + j = 0.
j increases from 0 to i, so i + j increases by 1 each step — producing consecutive numbers.
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
Program 34 uses the formula i + j per cell. Program 35 is a right-aligned continuous counter triangle.
System.out.print((i + j) + " ") keeps values separated on the same row. System.out.println() ends the row.
Replace 5 with max in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + … + (n+1) when i runs 0..n.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.

Did you know?

Each printed value is computed as i + j. With i = 0 and j = 0 the first row prints 0; row i = 2 prints 2, 3, 4 — a zero-based left-shifted increasing triangle.

Next: Right-Aligned Incremental Triangle

Move on to the right-aligned incremental number triangle in the Java number-pattern series.

Program 35 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