Continuous Number Triangle in C#

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

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:

Output
1
2 3
4 5 6
7 8 9 10
1

Complete C# Program

Use a counter k that increments every time you print a number.

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

1

Initialize a counter

k = 1 holds the next number to print.

Setup
2

Outer loop sets row length

Row i prints i numbers.

Row control
3

Inner loop prints k and increments

k++ prints k, then increments it so the next print continues the sequence.

Counter
=

Continuous sequence across rows

Total prints are triangular: \(1+2+\dots+n = n(n+1)/2\).

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)
        {
            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 k from a different number to shift the sequence
  • Print without trailing spaces by adding spaces only between numbers
  • Reset k each row to make rows restart from 1

Avoid

  • Declaring k inside the outer loop (that restarts the sequence every row)
  • Forgetting Console.WriteLine() after each row

Key Takeaways

1

A single counter k makes numbers continue across rows.

2

The outer loop controls how many values print per row.

3

Total prints follow \(n(n+1)/2\) for n rows.

4

Time complexity is \(O(n^2)\) as rows grow.

❓ Frequently Asked Questions

Row 1 prints 1, so the next values of the counter are 2 and 3 for row 2.
Change the outer loop limit to i <= 5 (or set rows = 5 in the variation).
O(n²) for n rows since total prints are n(n+1)/2.

Explore More C# Number Patterns!

Continuous counters also power patterns like Floyd’s triangle and sequential grids.

All Number Patterns →
Did you know?

The sum \(1+2+\dots+n\) is called a triangular number. It appears naturally in patterns where each row grows by one element.

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