An increasing number triangle using i + j - 1 prints a left-aligned triangle where each row starts with the row number and climbs by one.
Remember
Rule: print (i + j - 1) with a trailing space
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9 ← 5 rows
In C# the outer loop grows the row length; the inner loop prints i values per row. When j = 1, the formula simplifies to i — so every row starts with its own index.
Approach
How to Solve It
One nested-loop idea driven by the formula i + j - 1.
Method
Idea
Best for
Formula loops
Print i + j - 1 on each cell
Learning, interviews, exams
User-input rows
Same formula with a variable height
Practice / demos
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print (i + j - 1) and a space
print newline
Cheat sheet
Goal
Pattern
Grow the row
for (i = 1; i <= rows; i++)
Print i values
for (j = 1; j <= i; j++)
Cell value
Console.Write((i + j - 1) + " ");
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 row count and the triangle updates instantly — including the total value count.
Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 values
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
Trace
Worked Walkthrough — rows = 4
Trace each cell with the formula i + j - 1.
i
Values of j
Printed row
1
1+1-1 = 1
1
2
2, 3
2 3
3
3, 4, 5
3 4 5
4
4 … 7
4 5 6 7
Row i always prints i numbers, and the first value on that row is always i.
Code
C# Programs
Three complete programs: fixed height, user input, and a compact 3-row demo. Use View Output for sample results.
Example 1 — Fixed rows = 5
Nested loops print i + j - 1 with a trailing space on each cell.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 1; j <= i; j++)
Console.Write((i + j - 1) + " ");
Console.WriteLine();
}
}
}
}
Output
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
How It Works
1. Outer loop.i grows from 1 to 5 — each pass adds one more number to the row.
2. Formula.i + j - 1 starts each row at i, then climbs by one as j increases.
3. Newline.WriteLine() after the inner loop starts the next longer row.
Example 2 — User Input
Read the row count and apply the same formula for any height.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j;
Console.Write("Enter rows: ");
if (!int.TryParse(Console.ReadLine(), out rows) || rows < 1)
{
Console.WriteLine("Please enter a positive whole number.");
return;
}
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
Console.Write((i + j - 1) + " ");
Console.WriteLine();
}
}
}
}
A smaller fixed demo — same formula, easier to trace by hand.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 3;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
Console.Write((i + j - 1) + " ");
Console.WriteLine();
}
}
}
}
Output
1
2 3
3 4 5
How It Works
1. Same rules. Outer grows length; inner prints i + j - 1 with a space.
2. Quick check. Three rows end at 3 4 5.
Edge Cases & Pitfalls
Check these before calling the solution done.
wrong formula
Forget the - 1
Printing i + j starts row 1 at 2. Keep i + j - 1 so the first cell is 1.
missing space
Forget the trailing space
Numbers glue together (23). Always append " " in Write.
WriteLine early
WriteLine inside the inner loop
That puts each number on its own line. Call WriteLine() only after the inner loop.
rows = 1
Single 1
Output is just 1 (plus a trailing space).
rows ≤ 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)
Total printed values are 1 + 2 + … + n = n(n+1)/2, which is quadratic in n.
Remember
Key Takeaways
Rule: print i + j - 1 with a trailing space on each cell.
Shape: row i prints exactly i numbers and starts with i.
Write vs WriteLine: numbers stay on the line; WriteLine advances after each row.
Next step: Program 34 prints a similar triangle starting from 0.
One line: for each row i, print i values of i + j - 1 to build an increasing triangle from 1.
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.
Console.Write((i + j - 1) + " ") keeps values separated on the same row. WriteLine 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.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single 1.
🤔
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.