Shape Rule
Left-shifted triangle
Row i prints i numbers computed as i + j.

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.
Left-shifted triangle
Row i prints i numbers computed as i + j.
i = 0..max
for (i = 0; i <= max; i++) — zero-based outer loop, one growing row per iteration.
0..i
for (j = 0; j <= i; j++) — prints i + 1 values per row.
i + j
Each value is i + j — row i starts at i when j = 0.
2–9 max
Pick a max i value and draw the zero-based increasing triangle in the browser.
Complexity
Prints per row = i — total work scales as n².
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.
It combines zero-based nested loops with a compact formula — a step after Program 33’s i + j - 1 pattern.
Formula for each value.
Zero-based grow.
When i=0, j=0 → 0.
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().
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.
// 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 | Item | Type | Description |
|---|---|---|
max | int | Maximum outer-loop value — rows run from i = 0 to i = max. |
i | int | Outer loop — current row index (starts at 0). |
j | int | Inner loop — column index; runs 0..i per row. |
for i from 0 to max:
for j from 0 to i:
print (i + j) + space
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed formula | 0, 1 2, … | Learning and interviews |
| User-input max | sc.nextInt(); | Configurable triangle size |
| Compact trace | max = 2 on paper first | Debugging loop bounds |
| Goal | Pattern |
|---|---|
| Outer loop | for (i = 0; i <= max; i++) |
| Inner loop | for (j = 0; j <= i; j++) |
| Print value | System.out.print((i + j) + " "); |
| End the row | System.out.println(); |
| User input | sc.nextInt(); |
Same increasing triangle — different ways to control the max row index.
i = 0..maxZero-based outer loop
i + jStarts at 0
j = 0..ii + 1 values per row
j = 0 → iRow starts at row number
Reach for this pattern when teaching formula-based output, growing inner loops, and arithmetic in nested loops.
Natural follow-up after i + j - 1 — introduces zero-based loops with i + j.
Outer/inner bound practice with an immediate visual check.
Combine loops with ReadLine for a flexible row count.
Compare Program 33 (i + j - 1) and Program 35 (right-aligned counter) next.
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.
Choose a max i value between 2 and 9 and draw the increasing triangle in the browser.
Three complete Java programs — fixed max, user input, and a smaller trace demo. Click View Output to reveal sample console results.
Print six rows (i = 0..5) of the increasing triangle with the i + j formula.
max = 5Hard-coded maximum row index — ideal for first demos and screenshots.
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();
}
}
} 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.
Read the max row index with Scanner instead of hard-coding 5.
Read max with sc.nextInt() instead of hard-coding 5.
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();
}
} 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.
Run with max = 2 to trace every row on paper before scaling up.
max = 2Same nested-loop formula with a smaller max for quick tracing.
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();
}
}
} 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.
System.out is built in; use Scanner when reading input. Set loop variables i, j with max = 5.
for (i = 0; i <= max; i++) — zero-based outer loop, one growing row per iteration.
for (j = 0; j <= i; j++) — prints i + 1 values per row.
System.out.print((i + j) + " ") — each value from the arithmetic formula.
System.out.println() ends the row after the inner loop finishes.
Prints per row = i + 1 — O(n²) time, O(1) extra memory.
max = 5Trace each outer-loop value of i, inner-loop range, values printed, and full row output.
i | Inner range (j) | Values (i+j) | Row output |
|---|---|---|---|
0 | 0 | 0 | 0 |
1 | 0, 1 | 1, 2 | 1 2 |
2 | 0, 1, 2 | 2, 3, 4 | 2 3 4 |
3 | 0..3 | 3, 4, 5, 6 | 3 4 5 6 |
4 | 0..4 | 4, 5, 6, 7, 8 | 4 5 6 7 8 |
5 | 0..5 | 5, 6, 7, 8, 9, 10 | 5 6 7 8 9 10 |
Prints per row = i + 1 — total prints = (max+1)(max+2)/2 when i runs 0..max.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: change inner bound to j <= max and watch every row print the same width.
Foundation for formula-based triangles and left-shifted sequences starting at 1.
Example: continue to Program 35 for a right-aligned continuous counter triangle.
Practice System.out.print vs row newline without complex math.
Example: put System.out.println() inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use System.out.print(j + " ") between digits for wider spacing.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for max = 5 — total is 1+2+3+4+5+6 = 21.
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.
Why this pattern earns a permanent spot in beginner Java courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and stdio output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
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.
Small habits that keep number-pattern code clean.
Outer bound must be i <= max starting at i = 0 — row count is max + 1.
sc.hasNextInt()Avoid undefined behavior when the user types letters instead of a number.
Only call System.out.println() after the inner loop finishes the row.
Write the formula for each (i, j) pair before coding the loops.
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.
Mistakes that commonly break increasing number triangles.
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.
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.
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.
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) + " ").
Letters or empty input leave max uninitialized or unchanged.
→ Call sc.hasNextInt() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 0 — one value, one row.
Outer loop never runs when max < 0 — print nothing or show a message.
max < 0Treat as invalid; re-prompt instead of silent empty output.
Two rows: 0 and 1 2.
Unchecked Scanner input leaves max unset — call sc.hasNextInt() first.
Total prints = (max+1)(max+2)/2 — grows quadratically with max.
Try these variations to lock in the pattern.
i + j - 1 with i starting at 1j = 0, i + j = isc.hasNextInt() until max >= 0i + 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.max >= 0 for interactive programs; max = 0 prints a single 0.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().
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
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.
for (i = 0; i <= max; i++) in the outer loopfor (j = 0; j <= i; j++) prints i + 1 valuesSystem.out.print((i + j) + " ")sc.hasNextInt() instead of ignoring bad inputSystem.out.println() inside the inner loopi = 1 (skips the zero row)j <= max in the inner loop (prints a rectangle)Scanner input in user-facing demosmax = 0 edge casePrint the pattern the beginner-friendly way.
i + j
Definitioni = 0..max
Codej=0 → i
CodeSystem.out.println() after j loop
ShapeO(n²) time
AnalysisEach 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.
Move on to the right-aligned continuous counter triangle in the Java number-pattern series.
12 people found this page helpful