Continuous Number Triangle in C#

What You’ll Learn
How to print a continuous number triangle in C# where numbers keep increasing across rows:
1, then 2 3, then 4 5 6, then 7 8 9 10.
⭐ Pattern Output
For 4 rows, the pattern looks like this:
1
2 3
4 5 6
7 8 9 10Complete C# Program
Use a counter k that increments every time you print a number.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
int k = 1;
for (i = 1; i < 5; i++)
{
for (j = 1; j <= i; j++)
Console.Write(k++ + " ");
Console.WriteLine();
}
}
}
}🧠 How It Works
Initialize a counter
k = 1 holds the next number to print.
Outer loop sets row length
Row i prints i numbers.
Inner loop prints k and increments
k++ prints k, then increments it so the next print continues the sequence.
Continuous sequence across rows
Total prints are triangular: \(1+2+\dots+n = n(n+1)/2\).
Variation — User Input Rows
Let the user decide the number of rows at runtime:
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the number of rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
int k = 1;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
Console.Write(k++ + " ");
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Start
kfrom a different number to shift the sequence - Print without trailing spaces by adding spaces only between numbers
- Reset
keach row to make rows restart from 1
Avoid
- Declaring
kinside the outer loop (that restarts the sequence every row) - Forgetting
Console.WriteLine()after each row
Key Takeaways
A single counter k makes numbers continue across rows.
The outer loop controls how many values print per row.
Total prints follow \(n(n+1)/2\) for n rows.
Time complexity is \(O(n^2)\) as rows grow.
❓ Frequently Asked Questions
i <= 5 (or set rows = 5 in the variation).n(n+1)/2.Explore More C# Number Patterns!
Continuous counters also power patterns like Floyd’s triangle and sequential grids.
The sum \(1+2+\dots+n\) is called a triangular number. It appears naturally in patterns where each row grows by one element.
12 people found this page helpful
