Increasing Number Triangle Starting from 0 in Java

Beginner
⏱️ 9 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Formula + Loops

What You’ll Learn

The increasing number triangle using i + j prints 0, 1 2, 2 3 4, … — a natural follow-up after Program 33’s i + j - 1 triangle starting from 1. This tutorial covers the i + j formula, nested loops, a live preview, worked Java examples, edge cases, and complexity.

Shape Rule

Left-shifted triangle

Row i prints i numbers computed as i + j.

Outer Loop

i = 0..max

for (i = 0; i <= max; i++) — zero-based outer loop, one growing row per iteration.

Inner Loop (j)

0..i

for (j = 0; j <= i; j++) — prints i + 1 values per row.

Formula

i + j

Each value is i + j — row i starts at i when j = 0.

Live Preview

2–9 max

Pick a max i value and draw the zero-based increasing triangle in the browser.

O(n²)

Complexity

Prints per row = i — total work scales as .

Introduction

A left-shifted increasing number triangle prints values from the formula i + j on each row. With max = 5, you get 0, 1 2, 2 3 4, and so on.

In Java you use nested loops: outer i = 0..max, inner j = 0..i, printing (i + j) with a trailing space.

Why it matters?

It combines zero-based nested loops with a compact formula — a step after Program 33’s i + j - 1 pattern.

Key Highlights

i + j

Formula for each value.

Inner j <= i

Zero-based grow.

Starts at 0

When i=0, j=0 → 0.

Series Foundation

Follow Program 33; continue to Program 35 (right-aligned counter) next.

In short: outer loop i = 0..max, inner j = 0..i, print i + j with a space, then System.out.println().

📝 Problem & Approach

Given max = 5, print a zero-based left-shifted increasing triangle: for each row i, print j = 0..i values of i + j separated by spaces.

Java
// max = 5 (i runs 0..5)
// 0
// 1 2
// 2 3 4
// 3 4 5 6
// 4 5 6 7 8
// 5 6 7 8 9 10

Inputs & Outputs

ItemTypeDescription
maxintMaximum outer-loop value — rows run from i = 0 to i = max.
iintOuter loop — current row index (starts at 0).
jintInner loop — column index; runs 0..i per row.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Fixed formula0, 1 2, …Learning and interviews
User-input maxsc.nextInt();Configurable triangle size
Compact tracemax = 2 on paper firstDebugging loop bounds

⚡ Quick Reference

GoalPattern
Outer loopfor (i = 0; i <= max; i++)
Inner loopfor (j = 0; j <= i; j++)
Print valueSystem.out.print((i + j) + " ");
End the rowSystem.out.println();
User inputsc.nextInt();

📋 Fixed vs User Input vs Compact Demo

Same increasing triangle — different ways to control the max row index.

Outer loop
i = 0..max

Zero-based outer loop

Formula
i + j

Starts at 0

Inner loop
j = 0..i

i + 1 values per row

Learning tip
j = 0 → i

Row starts at row number

Context

When This Pattern Shows Up

Reach for this pattern when teaching formula-based output, growing inner loops, and arithmetic in nested loops.

  1. After Program 33

    Natural follow-up after i + j - 1 — introduces zero-based loops with i + j.

  2. Nested-loop warm-up

    Outer/inner bound practice with an immediate visual check.

  3. Console I/O practice

    Combine loops with ReadLine for a flexible row count.

  4. Gateway to variants

    Compare Program 33 (i + j - 1) and Program 35 (right-aligned counter) next.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.

🔮 Live Preview

Choose a max i value between 2 and 9 and draw the increasing triangle in the browser.

Try 4, 5, or 7. Max up to 9 in this preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete Java programs — fixed max, user input, and a smaller trace demo. Click View Output to reveal sample console results.

📚 Getting Started

Print six rows (i = 0..5) of the increasing triangle with the i + j formula.

Example 1 — Fixed max = 5

Hard-coded maximum row index — ideal for first demos and screenshots.

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

When i = 0, the inner loop prints 0+0 = 0. When i = 4, it prints 4, 5, 6, 7, 8 — output 4 5 6 7 8.

📈 User Input

Read the max row index with Scanner instead of hard-coding 5.

Example 2 — User input max

Read max with sc.nextInt() instead of hard-coding 5.

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

Same formula core as Example 1; only max comes from user input instead of being hard-coded as 5. Non-numeric input leaves max unset if you call nextInt() without checking hasNextInt() — always check it in safer labs.

⚡ Smaller Demo

Run with max = 2 to trace every row on paper before scaling up.

Example 3 — Compact max = 2

Same nested-loop formula with a smaller max for quick 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

Only max changes from 5 to 2 — the nested-loop formula stays identical. Trace i = 0, 1, 2 on paper to see how each row adds one more value.

🧠 How the Algorithm Prints Rows

1

Set up

System.out is built in; use Scanner when reading input. Set loop variables i, j with max = 5.

Setup
2

Outer loop walks rows

for (i = 0; i <= max; i++) — zero-based outer loop, one growing row per iteration.

Row
3

Inner loop (j)

for (j = 0; j <= i; j++) — prints i + 1 values per row.

Grow
4

Print formula

System.out.print((i + j) + " ") — each value from the arithmetic formula.

Formula
5

New line

System.out.println() ends the row after the inner loop finishes.

Break
=

Increasing triangle complete

Prints per row = i + 1O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — max = 5

Trace each outer-loop value of i, inner-loop range, values printed, and full row output.

iInner range (j)Values (i+j)Row output
0000
10, 11, 21 2
20, 1, 22, 3, 42 3 4
30..33, 4, 5, 63 4 5 6
40..44, 5, 6, 7, 84 5 6 7 8
50..55, 6, 7, 8, 9, 105 6 7 8 9 10

Prints per row = i + 1 — total prints = (max+1)(max+2)/2 when i runs 0..max.

Use Cases

Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.

1. Teaching Nested Loops

Clearest visual proof that outer and inner bounds interact.

Example: change inner bound to j <= max and watch every row print the same width.

2. Pattern Series Base

Foundation for formula-based triangles and left-shifted sequences starting at 1.

Example: continue to Program 35 for a right-aligned continuous counter triangle.

3. Console Formatting Drills

Practice System.out.print vs row newline without complex math.

Example: put System.out.println() inside the inner loop by mistake.

4. Padding character

Add spaces between digits once the two-loop structure works.

Example: use System.out.print(j + " ") between digits for wider spacing.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed numbers for max = 5 — total is 1+2+3+4+5+6 = 21.

6. Input Validation Labs

Pair the pattern with sc.hasNextInt() checks and non-negative max validation.

Example: reject max <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.

Advantages

Why this pattern earns a permanent spot in beginner Java courses.

  1. 1. Instant Visual Feedback

    Wrong bounds show up immediately as a broken staircase.

  2. 2. Minimal Concepts

    Only loops and stdio output — no arrays or math libraries.

  3. 3. Easy to Extend

    Invert, center, hollow, or change the fill character with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace i and j on paper for max = 2 before coding — watch how row i starts at i when j = 0.

Usage Tips

Small habits that keep number-pattern code clean.

  1. 1. Zero-Based Outer Loop

    Outer bound must be i <= max starting at i = 0 — row count is max + 1.

  2. 2. Call sc.hasNextInt()

    Avoid undefined behavior when the user types letters instead of a number.

  3. 3. Keep newline outside inner loop

    Only call System.out.println() after the inner loop finishes the row.

  4. 4. Trace i + j on Paper

    Write the formula for each (i, j) pair before coding the loops.

  5. 5. Dry-Run max = 2

    Trace i = 0..2 on paper before coding the full max = 5 demo.

Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put System.out.println() inside the inner loop.

Common Pitfalls

Mistakes that commonly break increasing number triangles.

  1. 1. Newline Inside the Inner Loop

    Each digit lands on its own line — you get a column, not a triangle.

    → Use System.out.print((i + j) + " "); System.out.println() only after the inner loop.

  2. 2. Wrong Formula

    Using i + j - 1 or starting at i = 1 shifts the triangle — it no longer starts at 0.

    → Keep i + j with i = 0..max and j = 0..i.

  3. 3. Wrong Inner Bound

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

    → Keep for (j = 0; j <= i; j++) so row i prints i + 1 values.

  4. 4. Missing Trailing Space

    Printing numbers without a space makes multi-digit values run together on wider rows.

    → Append a space after each number: System.out.print((i + j) + " ").

  5. 5. Unchecked Scanner input

    Letters or empty input leave max uninitialized or unchanged.

    → Call sc.hasNextInt() and re-prompt on failure.

Edge Cases

Check these inputs before calling the solution done.

max = 0

Single zero row

Output is just 0 — one value, one row.

max = -1

Empty pattern

Outer loop never runs when max < 0 — print nothing or show a message.

Negative

max < 0

Treat as invalid; re-prompt instead of silent empty output.

max = 1

Smallest triangle

Two rows: 0 and 1 2.

Bad input

Non-numeric Scanner input

Unchecked Scanner input leaves max unset — call sc.hasNextInt() first.

Large rows

Large row count

Total prints = (max+1)(max+2)/2 — grows quadratically with max.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Right-aligned counter

  • Continue with Program 35
  • Continuous counter with leading spaces

2. Triangle from 1

  • Review Program 33
  • Formula i + j - 1 with i starting at 1

3. Row starts at i

  • Prove on paper: when j = 0, i + j = i
  • Each row adds one more consecutive number

4. Safe input loop

  • Call sc.hasNextInt() until max >= 0
  • Then draw the triangle

Notes

  • Formula rule. Each value is i + j. Inner loop runs j = 0..i — row i prints i + 1 numbers.
  • System.out.print((i + j) + " ") stays on the line; System.out.println() advances — mix them carefully.
  • Validate max >= 0 for interactive programs; max = 0 prints a single 0.
  • When j = 0, the value is always i — compare with Program 33 where the formula is i + j - 1.

Quick Takeaway: outer loop i = 0..max, inner j = 0..i, print i + j, then System.out.println().

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(n²)O(1)
Smaller demo (Example 3)O(n²)O(1)
Wrap Up

🎉 Conclusion

The increasing number triangle starting from 0 is a compact lesson in zero-based nested loops: compute each value with i + j, grow the inner bound to i, and end each row with System.out.println(). Master the fixed-max version, then try user input and a smaller trace demo.

Practice the three examples above, then continue to Program 35 for the right-aligned continuous counter triangle.

Outer loop must start at i = 0 — validate max when reading with Scanner.

💡 Best Practices

✅ Do

  • Use for (i = 0; i <= max; i++) in the outer loop
  • Inner: for (j = 0; j <= i; j++) prints i + 1 values
  • Formula: System.out.print((i + j) + " ")
  • Call sc.hasNextInt() instead of ignoring bad input
  • State O(n²) time when asked about complexity

❌ Don’t

  • Call System.out.println() inside the inner loop
  • Start outer loop at i = 1 (skips the zero row)
  • Use j <= max in the inner loop (prints a rectangle)
  • Ignore bad Scanner input in user-facing demos
  • Skip the max = 0 edge case

Key Takeaways

Knowledge Unlocked

Five things to remember about this zero-based triangle

Print the pattern the beginner-friendly way.

5
Core concepts
02

Zero-based

i = 0..max

Code
0 03

Row start

j=0 → i

Code
04

Row break

System.out.println() after j loop

Shape
O 05

Complexity

O(n²) time

Analysis

❓ 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 digits 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(), or wrap nextInt in try/catch — see Example 2 notes.
Only one row prints — a single 0.

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.

Continue to Program 35

Move on to the right-aligned continuous counter 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