Three complete programs: fixed max = 5, cin input, and a compact max = 2 demo. Use View Output to reveal sample results.
Example 1 — Fixed max = 5
Hard-coded max — zero-based loops print i + j with a trailing space.
C++
#include <iostream>
using namespace std;
int main()
{
int i, j;
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= i; j++)
cout << i + j << " ";
cout << "\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 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 cout << "\n" only after the inner loop finishes.
Example 2 — User Input Max
Read max with cin, validate, then use the same formula core.
C++
#include <iostream>
using namespace std;
int main()
{
int max;
int i, j;
cout << "Enter max i: ";
if (!(cin >> max) || max < 0)
{
cout << "Please enter a non-negative integer.\n";
return 1;
}
for (i = 0; i <= max; i++)
{
for (j = 0; j <= i; j++)
cout << i + j << " ";
cout << "\n";
}
return 0;
}
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. Reject failed reads and negative values before printing.
2. Same core.i + j matches Example 1 — only max comes from the user.
3. Safer input tip. Cap demos for readable output:
Safer input
if (!(cin >> max) || max < 0 || max > 9)
{
cout << "Enter a whole number from 0 to 9.\n";
return 1;
}
Example 3 — Compact max = 2
Same structure with only three rows — easy to confirm zero-based indexing on paper.
C++
#include <iostream>
using namespace std;
int main()
{
int max = 2;
int i, j;
for (i = 0; i <= max; i++)
{
for (j = 0; j <= i; j++)
cout << i + j << " ";
cout << "\n";
}
return 0;
}
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 i = 0..max with i + j.
j <= max
Rectangle instead of triangle
Inner bound j <= max makes every row the same width. Use j <= i.
no space
Glued numbers
Without << " ", values run together (12). Always print a trailing space.
\n inside
Broken rows
If cout << "\n" sits inside the inner loop, you get one number per line. Call it only after the loop.
max = 0
Single value
Output is just 0. A good sanity check for input validation.
cin
Check the stream
Validate cin >> max before looping — a failed read leaves max unset or stale.
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 → 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 spaces in the inner loop; cout << "\n" 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, print i + j for j = 0..i with spaces, then newline.
Frequently Asked Questions
An increasing triangle from 0: for max=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.
cout << i + j << " " keeps values separated on the same row. cout << "\n" ends the row.
After prompting, use if (!(cin >> max) || max < 0) to reject bad input before printing.
O(n²) for max = 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.