An increasing number triangle from 0 uses zero-based loops and the formula i + j. The first cell is 0, and each row starts with its own i value.
Remember
Rule: print (i + j) with a trailing space (i, j from 0)
0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10 ← max = 5
In C# both loops start at 0. The outer loop runs while i <= max, so max = 5 produces six rows. Row i prints i + 1 values.
Approach
How to Solve It
One nested zero-based loop idea driven by i + j.
Method
Idea
Best for
Formula loops
Print i + j on each cell
Learning, interviews, exams
User-input max
Same formula with a variable upper bound
Practice / demos
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
Grow the row (0-based)
for (i = 0; i <= max; i++)
Print i + 1 values
for (j = 0; j <= i; j++)
Cell value
Console.Write((i + j) + " ");
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each number + space
Console.WriteLine
Ends the current line
After the inner loop
Try it
Live Preview
Change the max index and the triangle updates instantly — including row and value counts.
Whole numbers from 0 to 8. 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 cell with the formula i + j (both loops start at 0).
i
Values of j
Printed row
0
0+0 = 0
0
1
1, 2
1 2
2
2, 3, 4
2 3 4
3
3 … 6
3 4 5 6
Row i prints i + 1 numbers, and the first value on that row is always i.
Code
C# Programs
Three complete programs: fixed max, user input, and a compact max = 2 demo. Use View Output for sample results.
Example 1 — Fixed max = 5
Zero-based nested loops print i + j with a trailing space on each cell.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 0; i <= 5; i++)
{
for (j = 0; j <= i; j++)
Console.Write((i + j) + " ");
Console.WriteLine();
}
}
}
}
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.i runs from 0 to 5 — six rows, each one value longer than the last.
2. Formula.i + j starts each row at i, then climbs as j increases from 0 to i.
3. Newline.WriteLine() after the inner loop starts the next longer row.
Example 2 — User Input
Read the max index and apply the same formula for any upper bound.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int max;
int i, j;
Console.Write("Enter max i: ");
if (!int.TryParse(Console.ReadLine(), out max) || max < 0)
{
Console.WriteLine("Please enter a non-negative whole number.");
return;
}
for (i = 0; i <= max; i++)
{
for (j = 0; j <= i; j++)
Console.Write((i + j) + " ");
Console.WriteLine();
}
}
}
}
Output (when user enters 2)
Enter max i: 2
0
1 2
2 3 4
How It Works
1. Validate max.TryParse rejects non-numeric input; require max >= 0.
2. Same formula.max = 2 prints three rows ending at 2 3 4.
Example 3 — Compact max = 2
A smaller fixed demo — same zero-based idea, easier to trace by hand.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int max = 2;
int i, j;
for (i = 0; i <= max; i++)
{
for (j = 0; j <= i; j++)
Console.Write((i + j) + " ");
Console.WriteLine();
}
}
}
}
Output
0
1 2
2 3 4
How It Works
1. Same rules. Outer grows length from 0; inner prints i + j with a space.
2. Quick check. Three rows end at 2 3 4.
Edge Cases & Pitfalls
Check these before calling the solution done.
1-based habit
Start loops at 1 instead of 0
That skips the leading 0 and matches Program 33’s shape differently. Keep i and j from 0.
wrong bound
Use i < max instead of i <= max
You lose the last row. The outer loop must include max.
missing space
Forget the trailing space
Numbers glue together (12). Always append " " in Write.
max = 0
Single 0
Output is just 0 — one value, one row.
max < 0
Empty output
The outer loop never runs. Validate and prompt again for clearer UX.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / compact (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
When i runs 0..n there are n + 1 rows and (n+1)(n+2)/2 printed values — quadratic in n.
Remember
Key Takeaways
Rule: print i + j with a trailing space; start both loops at 0.
Shape: row i prints i + 1 numbers and starts with i.
Write vs WriteLine: numbers stay on the line; WriteLine advances after each row.
Next step: Program 35 prints a right-aligned incremental number triangle.
One line: for each i from 0 to max, print i + 1 values of i + j to build an increasing triangle from 0.
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.
Console.Write((i + j) + " ") keeps values separated on the same row. WriteLine ends the row.
Replace 5 with max in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + (n+1) when i runs 0..n.
Prefer int.TryParse(Console.ReadLine(), out max) so bad input does not throw FormatException.
Only one row prints — a single 0.
🤔
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.