C# Number Triangle Pattern (Starting from 0)

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

What Is This Pattern?

An increasing number triangle from 0 uses zero-based loops and the formula i + j. The first cell is 0, and each row starts with its own i value.

Remember
Rule: print (i + j) with a trailing space (i, j from 0)

0
1 2
2 3 4
3 4 5 6
4 5 6 7 8
5 6 7 8 9 10   ← max = 5

In C# both loops start at 0. The outer loop runs while i <= max, so max = 5 produces six rows. Row i prints i + 1 values.

How to Solve It

One nested zero-based loop idea driven by i + j.

MethodIdeaBest for
Formula loopsPrint i + j on each cellLearning, interviews, exams
User-input maxSame formula with a variable upper boundPractice / demos

Pseudocode

Pseudocode
for i from 0 to max:
    for j from 0 to i:
        print (i + j) and a space
    print newline

Cheat sheet

GoalPattern
Grow the row (0-based)for (i = 0; i <= max; i++)
Print i + 1 valuesfor (j = 0; j <= i; j++)
Cell valueConsole.Write((i + j) + " ");
End the rowConsole.WriteLine();

Write vs WriteLine

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

Live Preview

Change the max index and the triangle updates instantly — including row and value counts.

Whole numbers from 0 to 8. Tap a chip or type a value — the preview redraws as you go.

Live result max 5 · 6 rows · 21 values
0 
1 2 
2 3 4 
3 4 5 6 
4 5 6 7 8 
5 6 7 8 9 10 

Worked Walkthrough — max = 3

Trace each cell with the formula i + j (both loops start at 0).

iValues of jPrinted row
00+0 = 00
11, 21 2
22, 3, 42 3 4
33 … 63 4 5 6

Row i prints i + 1 numbers, and the first value on that row is always i.

C# Programs

Three complete programs: fixed max, user input, and a compact max = 2 demo. Use View Output for sample results.

Example 1 — Fixed max = 5

Zero-based nested loops print i + j with a trailing space on each cell.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;

            for (i = 0; i <= 5; i++)
            {
                for (j = 0; j <= i; j++)
                    Console.Write((i + j) + " ");

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i runs from 0 to 5 — six rows, each one value longer than the last.

2. Formula. i + j starts each row at i, then climbs as j increases from 0 to i.

3. Newline. WriteLine() after the inner loop starts the next longer row.

Example 2 — User Input

Read the max index and apply the same formula for any upper bound.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int max;
            int i, j;

            Console.Write("Enter max i: ");
            if (!int.TryParse(Console.ReadLine(), out max) || max < 0)
            {
                Console.WriteLine("Please enter a non-negative whole number.");
                return;
            }

            for (i = 0; i <= max; i++)
            {
                for (j = 0; j <= i; j++)
                    Console.Write((i + j) + " ");

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

2. Same formula. max = 2 prints three rows ending at 2 3 4.

Example 3 — Compact max = 2

A smaller fixed demo — same zero-based idea, easier to trace by hand.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int max = 2;
            int i, j;

            for (i = 0; i <= max; i++)
            {
                for (j = 0; j <= i; j++)
                    Console.Write((i + j) + " ");

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same rules. Outer grows length from 0; inner prints i + j with a space.

2. Quick check. Three rows end at 2 3 4.

Edge Cases & Pitfalls

Check these before calling the solution done.

1-based habit

Start loops at 1 instead of 0

That skips the leading 0 and matches Program 33’s shape differently. Keep i and j from 0.

wrong bound

Use i < max instead of i <= max

You lose the last row. The outer loop must include max.

missing space

Forget the trailing space

Numbers glue together (12). Always append " " in Write.

max = 0

Single 0

Output is just 0 — one value, one row.

max < 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
Fixed / compact (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

When i runs 0..n there are n + 1 rows and (n+1)(n+2)/2 printed values — quadratic in n.

Key Takeaways

  • Rule: print i + j with a trailing space; start both loops at 0.
  • Shape: row i prints i + 1 numbers and starts with i.
  • Write vs WriteLine: numbers stay on the line; WriteLine advances after each row.
  • Next step: Program 35 prints a right-aligned incremental number triangle.

One line: for each i from 0 to max, print i + 1 values of i + j to build an increasing triangle from 0.

Frequently Asked Questions

Because the loops start at i = 0 and j = 0, so i + j = 0.
j increases from 0 to i, so i + j increases by 1 each step — producing consecutive numbers.
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
Program 34 uses the formula i + j per cell. Program 35 is a right-aligned continuous counter triangle.
Console.Write((i + j) + " ") keeps values separated on the same row. WriteLine ends the row.
Replace 5 with max in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + (n+1) when i runs 0..n.
Prefer int.TryParse(Console.ReadLine(), out max) so bad input does not throw FormatException.
Only one row prints — a single 0.

Did you know?

Each printed value is computed as i + j. With i = 0 and j = 0 the first row prints 0; row i = 2 prints 2, 3, 4 — a zero-based left-shifted increasing triangle.

Next: Right-Aligned Incremental Triangle

Pad with spaces and print a continuous counter on each row.

Program 35 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.

6 people found this page helpful