An ascending number triangle prints digits 1 through i on row i, growing one digit per line.
Remember
Rule: on row i, print 1..i with Console.Write
1
12
123
1234
12345 ← rows = 5
In C# the outer loop runs i from 1 to rows; the inner loop prints j from 1 to i, then WriteLine ends the row.
Approach
How to Solve It
One nested loop: grow the row count, print 1 through i each time.
Method
Idea
Best for
Tight digits
Console.Write(j) for j = 1..i
Learning, interviews, exams
Spaced digits
Console.Write(j + " ")
Easier reading on screen
Pseudocode
Pseudocode
for i from 1 to rows:
for j from 1 to i:
print j
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (i = 1; i <= rows; i++)
Print 1..i
for (j = 1; j <= i; j++) Console.Write(j);
Spaced digits
Console.Write(j + " ");
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each digit on the row
Console.WriteLine
Ends the current line
After the inner loop finishes a row
Try it
Live Preview
Change the row count and the ascending triangle updates instantly.
Whole numbers from 1 to 9 (keeps single-digit rows). Tap a chip or type a value — the preview redraws as you go.
Live result5 rows · 15 digits
1
12
123
1234
12345
Trace
Worked Walkthrough — rows = 4
Trace how the inner bound grows with i.
i
Inner j
Printed row
1
1
1
2
1..2
12
3
1..3
123
4
1..4
1234
Total digits for n rows = n(n+1)/2. Each row is one digit longer than the previous.
Code
C# Programs
Three complete programs: fixed 5 rows, user-input rows, and spaced digits. Use View Output for sample results.
Example 1 — Fixed rows = 5
Nested loops print digits tightly with Console.Write(j).
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
Console.Write(j);
Console.WriteLine();
}
}
}
}
Output
1
12
123
1234
12345
How It Works
1. Outer loop.i runs from 1 to 5 — one row per value.
2. Inner loop. Print j from 1 to i so the row length matches i.
3. New line.WriteLine after the inner loop starts the next row.
Example 2 — User Input (rows)
Read the row count and build the same growing triangle.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows, i, j;
Console.Write("Enter the number of 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(j);
Console.WriteLine();
}
}
}
}
2. Same triangle. Only the outer bound changes from the literal 5.
Example 3 — Spaced Digits
Add a space after each digit for easier reading.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
Console.Write(j + " ");
Console.WriteLine();
}
}
}
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
How It Works
1. Same loops. Bounds stay i = 1..rows and j = 1..i.
2. Only the print changes.j + " " separates digits without changing the triangle shape.
Edge Cases & Pitfalls
Check these before calling the solution done.
i--
Outer loop counts down
You get Program 1’s shrinking triangle (12345, 1234, …). Keep i from 1 up to rows.
WriteLine
WriteLine inside the inner loop
That puts every digit on its own line. Call WriteLine only after the column loop.
print i
Print i instead of j
You get 1, 22, 333, 4444… Print the column index j for 1..i.
rows = 1
Single row
Output is just 1 — one digit on one line.
rows > 9
Multi-digit values
Printing 10+ without spaces runs digits together. Cap at 9 or use spaces / fixed width.
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 / spaced (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
Total printed digits are 1 + 2 + … + n = n(n+1)/2, so work is quadratic in the row count.
Remember
Key Takeaways
Rule: on row i, print digits 1..i — the triangle grows upward.
Loops: outer i = 1..rows, inner j = 1..i.
Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
Next step: Program 6 prints a reverse ascending triangle (5, 45, 345…).
One line: for each row i from 1 to rows, print 1 through i.
Frequently Asked Questions
The outer loop runs i from 1 to rows. For each row i, the inner loop runs j from 1 to i and prints j. Row 1 prints 1, row 2 prints 12, and so on.
The outer loop counts up and the inner bound equals i. When i increases, each row prints one more digit than the previous row.
Console.Write stays on the same line. Console.WriteLine ends the current line. Digits use Write; the row break uses WriteLine after the inner loop.
Program 4 prints rows..i in descending order (54321, 5432, …). Program 5 prints 1..i in ascending order — a growing triangle instead of a shrinking one.
Replace 5 with rows in the outer loop bound — see Example 2.
Count the outer loop down: for (i = rows; i >= 1; i--). Keep the inner loop as for (j = 1; j <= i; j++) — that is Program 1.
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 digit 1 on one line.
🤔
Did you know?
Row i prints digits 1 through i. The outer loop counts up from 1 to rows, so each row grows by one digit — total prints = n(n+1)/2.