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.
Approach
How to Solve It
Walk i from 0 to max; for each row print j = 0..i values of i + j.
Method
Idea
Best for
Formula i + j
Zero-based nested loops; row i starts at i
Learning, interviews, exams
User-input max
Same formula; read upper bound with Scanner
When 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
Goal
Pattern
Walk rows
for (int i = 0; i <= max; i++)
Print i+1 values
for (int j = 0; j <= i; j++)
Compute value
System.out.print((i + j) + " ");
End the row
System.out.println();
Row start check
When j = 0, value equals i
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each number plus trailing space
System.out.println
Ends the current line
After the inner loop finishes
Print values without a newline, then end the row once.
Try it
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 resultmax = 5 · 6 rows
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
Trace
Worked Walkthrough — max = 5
Trace i, the inner range j = 0..i, and i + j for each cell.
i
Values of i + j
Printed row
0
0
0
1
1, 2
1 2
2
2, 3, 4
2 3 4
3
3 … 6
3 4 5 6
4
4 … 8
4 5 6 7 8
5
5 … 10
5 6 7 8 9 10
Total values = 1 + 2 + … + 6 = 21 — formula (max+1)(max+2)/2.
Code
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();
}
}
}
Output
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10
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();
}
}
Output (when user enters 2)
Enter max i: 2
0
1 2
2 3 4
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();
}
}
}
Output
0
1 2
2 3 4
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.