Continuous Numbers with Decreasing Row Length in C#

What You’ll Learn
How to print a triangle where numbers increase continuously, but each next row prints one fewer number than the previous row.
We use a counter k that keeps increasing, and an inner loop that prints rows - i + 1 numbers per row.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1 2 3 4 5
6 7 8 9
10 11 12
13 14
15Complete C# Program
The outer loop controls the number of rows. The inner loop prints fewer numbers each row and increments k continuously.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int i, j;
int k = 1;
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= i; j--)
Console.Write("{0,3}", k++);
Console.WriteLine();
}
}
}
}🧠 How It Works
Initialize the counter
k starts at 1 and increases every time a value is printed.
Outer loop (rows)
i runs from 1 to rows, printing one line each iteration.
Inner loop (decreasing count)
for (j = rows; j >= i; j--) prints rows-i+1 numbers on the current line.
Fixed-width formatting
{0,3} keeps columns aligned as numbers grow to 2 digits and beyond.
Decreasing row-length triangle
Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²).
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)
{
int rows;
int i, j;
int k = 1;
Console.Write("Enter number of rows: ");
if (!int.TryParse(Console.ReadLine(), out rows) || rows <= 0)
{
Console.WriteLine("Please enter a positive integer.");
return;
}
for (i = 1; i <= rows; i++)
{
for (j = rows; j >= i; j--)
Console.Write("{0,3}", k++);
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Add spaces or separators between numbers (or keep the fixed width formatting)
- Print an increasing row-length triangle by changing the inner loop condition
- Reset
keach row to print 1..N repeatedly - Use a custom start value by setting
kto something else
Avoid
- Using too small a width when values become two digits
- Forgetting
Console.WriteLine()after the inner loop - Not validating user input for rows
Key Takeaways
A single counter k makes numbers continuous across rows.
Row length decreases because the inner loop runs from rows down to i.
Fixed-width output keeps the triangle aligned in the console.
Total numbers printed are triangular: n(n+1)/2.
❓ Frequently Asked Questions
k continues from the previous row. After printing 1..5, the next number is 6.k to your start value (e.g., int k = 100;).Console.WriteLine() after the inner loop. That’s what moves the cursor to the next row.Explore More C# Number Patterns!
Try both increasing and decreasing row-length patterns to strengthen your loop logic.
The total numbers printed for n rows is the triangular number n(n+1)/2. For 5 rows, that’s 15 (ending at 15).
12 people found this page helpful
