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.
Approach
How to Solve It
Grow i from 1 to rows; for each row print j = 1..i values of i + j − 1.
Method
Idea
Best for
Formula i + j − 1
Nested loops; row i starts at i
Learning, interviews, exams
User-input rows
Same formula; read height with Scanner
When 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
Goal
Pattern
Walk rows
for (int i = 1; i <= rows; i++)
Print i values
for (int j = 1; j <= i; j++)
Compute value
System.out.print((i + j - 1) + " ");
End the row
System.out.println();
Row start check
When j = 1, 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 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 resultrows = 5 · 15 values
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
Trace
Worked Walkthrough — rows = 5
Trace i, the inner range j = 1..i, and i + j − 1 for each cell.
i
Values of i + j − 1
Printed row
1
1
1
2
2, 3
2 3
3
3, 4, 5
3 4 5
4
4 … 7
4 5 6 7
5
5 … 9
5 6 7 8 9
Total values = 1 + 2 + … + 5 = 15 — the triangular number n(n+1)/2.
Code
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();
}
}
}
Output
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
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();
}
}
Output (when user enters 3)
Enter rows: 3
1
2 3
3 4 5
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();
}
}
}
Output
1
2 3
3 4 5
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Increasing from 1 (Examples 1–3)
O(n²)
O(1)
Total values = n(n+1)/2 — still quadratic. Only loop counters are stored.
Remember
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.