C# Continuous Number Triangle Pattern

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

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.

How to Solve It

One nested-loop idea with two useful variants — fixed start and custom start.

MethodIdeaBest for
Running counterk = 1 once; inner loop prints k++ for i timesLearning, interviews, exams
Custom startSame loops, but set k to any start valuePractice 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

GoalPattern
Grow the rowfor (i = 1; i <= rows; i++)
Print i numbersfor (j = 1; j <= i; j++)
Continue the sequenceConsole.Write(k++ + " ");
End the rowConsole.WriteLine();
Custom startint k = 10; before the loops

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach number k++ plus a space
Console.WriteLineEnds the current lineAfter the inner loop

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 result 4 rows · 10 numbers
1
2 3
4 5 6
7 8 9 10

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, the k values printed, and how k carries forward.

ik before rowPrinted rowCount
1111
222 32
344 5 63
477 8 9 104

Total numbers are 1+2+3+4 = 10 — the triangular number n(n+1)/2.

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();
            }
        }
    }
}

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();
            }
        }
    }
}

How It Works

1. Validate rows. TryParse rejects non-numeric input; require rows >= 1.

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();
            }
        }
    }
}

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.

Time and Space Complexity

ProgramTimeExtra 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.

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.

Next: Increasing Jump Number Triangle

Keep a growing triangle, but jump the counter by a larger step each row.

Program 21 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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