An increasing triangle from 0 prints values from the formula i + j with zero-based loops. Row i has i + 1 consecutive numbers starting at i.
Remember
Rule: for i from 0 to max
for j from 0 to i:
print (i + j) + space
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10 ← max = 5
Same growing shape as Program 33, but loops start at 0. Follows the triangle from 1 in Program 33; next is Program 35.
Approach
How to Solve It
Start with fixed max = 5, then generalize with scanf — optionally demo a smaller max for tracing.
Method
Idea
Best for
Fixed formula
Print i + j for j = 0..i, i = 0..max
Learning, interviews, exams
User input max
Same loops; read max with scanf
Reusable demos and labs
Pseudocode
Pseudocode
for i from 0 to max:
for j from 0 to i:
print (i + j) and a space
print newline
Cheat sheet
Goal
Pattern
Walk each row
for (i = 0; i <= max; i++)
Grow row length
for (j = 0; j <= i; j++)
Print value + space
printf("%d ", i + j);
End the row
printf("\n");
Start at 1 instead
printf("%d ", i + j - 1); with i = 1..rows — see Program 33
Printing Numbers vs Starting a New Line
API
Effect
Use for
printf("%d ", i + j)
Stays on the same line
Each value plus a separator space
printf("\n")
Ends the current line
After the inner loop
Print all values on the row without a newline, then end the row once.
Try it
Live Preview
Change the max index and the triangle updates instantly — rows run from i = 0 to i = max.
Whole numbers from 0 to 9. Tap a chip or type a value — the preview redraws as you go.
Live resultmax 5 · 6 rows · 21 values
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 = 3
Trace each outer-loop value of i from 0 to 3 and list the formula results for j = 0..i.
i
Values (i + j)
Printed row
0
0
0
1
1, 2
1 2
2
2, 3, 4
2 3 4
3
3, 4, 5, 6
3 4 5 6
With max = n you get n + 1 rows and (n + 1)(n + 2)/2 values (here 4 × 5 / 2 = 10). That triangular sum is why time is O(n²).
Code
C Programs
Three complete programs: fixed max, scanf input, and a small demo. Use View Output to reveal sample results.
Example 1 — Fixed max = 5
Hard-coded max index — classic i + j formula with zero-based loops.
C
#include <stdio.h>
int main(void)
{
int i, j;
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= i; j++)
printf("%d ", i + j);
printf("\n");
}
return 0;
}
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 grows i.i runs from 0 to 5 — six rows, each one value longer.
2. Inner loop prints the formula.j runs from 0 to i; each value is i + j plus a space.
3. Newline.printf("\n") ends the row after the inner loop.
When i = 0 you get 0; when i = 2 you get 2 3 4.
Example 2 — User Input Version
Read max with scanf and reject negative values. Same formula core.
C
#include <stdio.h>
int main(void)
{
int max;
int i, j;
printf("Enter max i: ");
scanf("%d", &max);
if (max < 0) max = 0;
for (i = 0; i <= max; i++)
{
for (j = 0; j <= i; j++)
printf("%d ", i + j);
printf("\n");
}
return 0;
}
Output (when user enters 2)
Enter max i: 2
0
1 2
2 3 4
How It Works
1. Prompt and clamp. Read max, then force a minimum of 0 so at least one row prints.
2. Same nested core. Only the hard-coded 5 becomes the user value.
3. Safer input tip. Unchecked scanf leaves max uninitialized on bad input. Prefer:
Safer input
if (scanf("%d", &max) != 1 || max < 0)
{
printf("Enter a whole number max of 0 or greater.\n");
return 1;
}
Example 3 — Compact max = 2
Same formula with a smaller max for quick paper tracing.
C
#include <stdio.h>
int main(void)
{
int max = 2;
int i, j;
for (i = 0; i <= max; i++)
{
for (j = 0; j <= i; j++)
printf("%d ", i + j);
printf("\n");
}
return 0;
}
Output
0
1 2
2 3 4
How It Works
1. Same structure. Only max changes from 5 to 2 — the formula stays i + j.
2. Trace on paper. For i = 1: 1+0 = 1, 1+1 = 2 → 1 2.
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 the 0 row
Starting i at 1 skips the leading 0. Keep for (i = 0; i <= max; i++).
wrong formula
Rows start at the wrong number
Using i + j - 1 shifts every value like Program 33. Keep i + j so the first cell is 0.
no space
Glued numbers
printf("%d", i + j) without a space merges values like 12. Always append a space in the format string.
\n inside
Broken rows
If printf("\n") sits inside the inner loop, each value lands on its own line. Call the newline only after the row finishes.
max = 0
Smallest triangle
Output is a single 0 — a good sanity check.
scanf
Check the return value
If scanf fails, max may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / input (Examples 1–2)
O(max²)
O(1)
Small demo (Example 3)
O(max²)
O(1)
With max = n, total values = (n + 1)(n + 2)/2 — quadratic in n. For max = 5 that is 21 printed numbers across 6 rows.
Remember
Key Takeaways
Rule: for each i from 0 to max, print i + j for j = 0..i.
Zero-based: starting at i = 0, j = 0 makes the first value 0.
Break the row: call printf("\n") only after the inner loop.
Complexity:O(n²) time from (n+1)(n+2)/2 values; O(1) extra space.
One line: for i from 0 to max, print i + j for j = 0..i, then printf("\n").
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.
printf("%d ", i + j) keeps values separated on the same row. printf("\n") ends the row.
Replace 5 with max in the outer loop bound — see Example 2. Rows run from i = 0 to i = max.
O(n²) for max = n because total prints are 1 + 2 + ... + (n+1) = (n+1)(n+2)/2.
Check scanf's return value and reject negative max so the outer loop has a valid range.
🤔
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.