A continuous number triangle prints a growing triangle where a single counter keeps going across rows — it never resets to 1.
Remember
Rule: k = 1 once; row i prints i values of k++
1
2 3
4 5 6
7 8 9 10 ← 4 rows
In C# you declare k = 1 once before the loops, print k++ in the inner loop for i numbers per row, then Console.WriteLine() ends each row. Put k inside the outer loop by mistake and every row restarts at 1.
Approach
How to Solve It
One nested-loop idea with two useful variants — fixed start and custom start.
Method
Idea
Best for
Running counter
k = 1 once; inner loop prints k++ for i times
Learning, interviews, exams
Custom start
Same loops, but set k to any start value
Practice variants
Pseudocode
Pseudocode
k = 1
for i from 1 to rows:
for j from 1 to i:
print k, then k = k + 1
print newline
Cheat sheet
Goal
Pattern
Grow the row
for (i = 1; i <= rows; i++)
Print i numbers
for (j = 1; j <= i; j++)
Continue the sequence
Console.Write(k++ + " ");
End the row
Console.WriteLine();
Custom start
int k = 10; before the loops
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each number k++ plus a space
Console.WriteLine
Ends the current line
After the inner loop
Try it
Live Preview
Change the row count and the continuous triangle updates instantly — including the total number count.
Whole numbers from 1 to 10. Tap a chip or type a value — the preview redraws as you go.
Live result4 rows · 10 numbers
1
2 3
4 5 6
7 8 9 10
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i, the k values printed, and how k carries forward.
i
k before row
Printed row
Count
1
1
1
1
2
2
2 3
2
3
4
4 5 6
3
4
7
7 8 9 10
4
Total numbers are 1+2+3+4 = 10 — the triangular number n(n+1)/2.
Code
C# Programs
Three complete programs: fixed height, row-count input, and a custom start value. Use View Output for sample results.
Example 1 — Fixed rows = 4
Declare k once; each row prints one more continuous number.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 4;
int i, j, k = 1;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write(k++ + " ");
}
Console.WriteLine();
}
}
}
}
Output
1
2 3
4 5 6
7 8 9 10
How It Works
1. Start the counter.k = 1 is set once before any loop, so it survives across rows.
2. Grow the row. Row i prints i numbers via Console.Write(k++ + " ").
3. Newline.WriteLine() after the inner loop starts the next longer row.
Example 2 — User Input
Read the row count at runtime. Prefer int.TryParse in real apps so bad input does not throw.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows;
int i, j, k = 1;
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(k++ + " ");
}
Console.WriteLine();
}
}
}
}
2. Same nested loops. Four rows print the continuous sequence 1 through 10.
Example 3 — Custom Start k = 10
Same shape — only the starting value of the counter changes.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 4;
int i, j, k = 10;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write(k++ + " ");
}
Console.WriteLine();
}
}
}
}
Output
10
11 12
13 14 15
16 17 18 19
How It Works
1. Shift the start.k = 10 before the loops; everything else matches Example 1.
2. Same carry-over. After row 1 prints 10, row 2 continues with 11 12.
Edge Cases & Pitfalls
Check these before calling the solution done.
reset k
k = 1 inside the outer loop
Every row restarts at 1. Keep k declared once before both loops.
WriteLine early
WriteLine(k++) inside the inner loop
That puts each number on its own line. Use Write(k++ + " "); call WriteLine() only after the row finishes.
pre vs post
++k instead of k++
Pre-increment prints the next value first. Prefer post-increment inside Write(k++ + " ").
rows = 1
Single 1
Output is just 1 — a good sanity check.
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
Nested loops (Examples 1–2)
O(n²)
O(1)
Custom start (Example 3)
O(n²)
O(1)
There are n rows printing 1+2+…+n numbers, so total work is n(n+1)/2.
Remember
Key Takeaways
Rule: set k = 1 once; row i prints i values of k++.
Carry over: declaring k outside the outer loop is what makes the sequence continuous.
Write vs WriteLine: numbers stay on the line; WriteLine advances after each row.
Next step: Program 21 jumps the counter by a growing amount each row.
One line: set k once, then for each row print i values of k++ with Write, then WriteLine.
Frequently Asked Questions
Because k is declared once (k = 1) before the loops and incremented with k++ every time a number is printed — its value carries over to the next row.
Row 1 prints 1, so k becomes 2. Row 2 prints k twice: 2, then 3 after k++.
k resets each row and the sequence restarts at 1 — you get a growing triangle of 1s, not a continuous counter.
Console.Write stays on the same line. Console.WriteLine ends the current line. Numbers use Write(k++ + " "); the row break uses WriteLine after the inner loop.
Total prints are 1+2+…+n = n(n+1)/2.
Yes. Set k = 10 (or any value) before the loops — the sequence continues from that start (see Example 3).
O(n²) for n rows because total Console.Write calls equal n(n+1)/2.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.
🤔
Did you know?
A single counter k starts at 1 and increments with k++ on every print — numbers continue across rows instead of restarting. Total prints still equal n(n+1)/2 for n rows.