Three complete programs: fixed max_n = 5, input() variant, and a compact max_n = 2 demo. Use View Output to reveal sample results.
Example 1 — Fixed max_n = 5
Hard-coded max — zero-based loops print i + j with a trailing space.
Python
for i in range(0, 6):
for j in range(0, i + 1):
print(i + j, end=" ")
print()
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 grows from 0. Row i prints exactly i + 1 numbers.
2. Formula fills the cells.i + j makes row i start at i and count upward.
3. Newline once. Call bare print() only after the inner loop finishes.
Example 2 — User Input Max
Read max_n with input(), validate, then use the same formula core.
Python
try:
max_n = int(input("Enter max i: "))
except ValueError:
print("Please enter a non-negative integer.")
raise SystemExit(1)
if max_n < 0:
print("Please enter a non-negative integer.")
raise SystemExit(1)
for i in range(0, max_n + 1):
for j in range(0, i + 1):
print(i + j, end=" ")
print()
Output (when user enters 3)
Enter max i: 3
0
1 2
2 3 4
3 4 5 6
How It Works
1. Prompt and validate. Catch ValueError; reject negative values before printing.
2. Same core.i + j matches Example 1 — only max_n comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input tip
if max_n < 0 or max_n > 9:
print("Enter a whole number from 0 to 9.")
raise SystemExit(1)
Example 3 — Compact max_n = 2
Same structure with only three rows — easy to confirm zero-based indexing on paper.
Python
max_n = 2
for i in range(0, max_n + 1):
for j in range(0, i + 1):
print(i + j, end=" ")
print()
Output
0
1 2
2 3 4
How It Works
1. Three rows.i = 0 → 0; i = 1 → 1 2; i = 2 → 2 3 4.
2. Trace on paper. If you start at i = 1, the leading 0 disappears and the shape matches Program 33 less cleanly.
3. Scale up next. Once the small demo is clear, use Examples 1–2 for max 5 or user input.
Edge Cases & Pitfalls
Check these before calling the solution done.
i = 1
Missing zero row
Starting at i = 1 or using i + j - 1 drops the leading 0. Keep range(0, max_n + 1) with i + j.
j to max_n
Rectangle instead of triangle
Inner range(0, max_n + 1) makes every row the same width. Use range(0, i + 1).
no space
Glued numbers
Without end=" ", values run together (12). Always print a trailing space.
print() inside
Broken rows
If bare print() sits inside the inner loop, you get one number per line. Call it only after the loop.
max_n = 0
Single value
Output is just 0. A good sanity check for input validation.
input()
Catch ValueError
Bare int(input()) crashes on non-numeric text — wrap it in try/except ValueError.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(n²)
O(1)
Compact max = 2 (Example 3)
O(n²)
O(1)
Row i prints i + 1 numbers. Total = 1 + 2 + … + (n+1) = (n+1)(n+2)/2 for max_n = n → O(n²) time. Only a few loop variables are needed.
Remember
Key Takeaways
Formula: each cell is i + j — first cell is 0 when both loops start at 0.
Zero-based width: inner loop runs j = 0..i so row i has i + 1 numbers.
Break the row: values with end=" " in the inner loop; bare print() after it finishes.
Complexity:O(n²) time from (n+1)(n+2)/2 prints; O(1) extra space.
One line: for each i from 0 to max_n, print i + j for j = 0..i with spaces, then print().
Frequently Asked Questions
An increasing triangle from 0: for max_n=5 you get 0 / 1 2 / 2 3 4 / 3 4 5 6 / 4 5 6 7 8 / 5 6 7 8 9 10 — each value is i + j with zero-based loops.
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.
print(i + j, end=" ") keeps values separated on the same row. print() ends the row.
Use try/except ValueError around int(input()) and require max_n >= 0 — see Example 2.
O(n²) for max_n = n because total prints are 1 + 2 + … + (n+1) = (n+1)(n+2)/2.
🤔
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.