Continuous Numbers with Decreasing Row Length in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops + Counter

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:

Output
 1  2  3  4  5
 6  7  8  9
10 11 12
13 14
15
1

Complete C# Program

The outer loop controls the number of rows. The inner loop prints fewer numbers each row and increments k continuously.

C#
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

1

Initialize the counter

k starts at 1 and increases every time a value is printed.

Counter
2

Outer loop (rows)

i runs from 1 to rows, printing one line each iteration.

Row control
3

Inner loop (decreasing count)

for (j = rows; j >= i; j--) prints rows-i+1 numbers on the current line.

Print count
4

Fixed-width formatting

{0,3} keeps columns aligned as numbers grow to 2 digits and beyond.

Alignment
=

Decreasing row-length triangle

Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²).

2

Variation — User Input Rows

Let the user decide the number of rows at runtime.

C#
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 k each row to print 1..N repeatedly
  • Use a custom start value by setting k to 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

1

A single counter k makes numbers continuous across rows.

2

Row length decreases because the inner loop runs from rows down to i.

3

Fixed-width output keeps the triangle aligned in the console.

4

Total numbers printed are triangular: n(n+1)/2.

❓ Frequently Asked Questions

Because k continues from the previous row. After printing 1..5, the next number is 6.
A total of n(n+1)/2 numbers. For 5 rows, that’s 15.
Yes. Set k to your start value (e.g., int k = 100;).
Use 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.

All Number Patterns →
Did you know?

The total numbers printed for n rows is the triangular number n(n+1)/2. For 5 rows, that’s 15 (ending at 15).

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful