C# Number Triangle Pattern (Consecutive Offset)

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

What Is This Pattern?

An increasing number triangle using i + j - 1 prints a left-aligned triangle where each row starts with the row number and climbs by one.

Remember
Rule: print (i + j - 1) with a trailing space

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9   ← 5 rows

In C# the outer loop grows the row length; the inner loop prints i values per row. When j = 1, the formula simplifies to i — so every row starts with its own index.

How to Solve It

One nested-loop idea driven by the formula i + j - 1.

MethodIdeaBest for
Formula loopsPrint i + j - 1 on each cellLearning, interviews, exams
User-input rowsSame formula with a variable heightPractice / demos

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print (i + j - 1) and a space
    print newline

Cheat sheet

GoalPattern
Grow the rowfor (i = 1; i <= rows; i++)
Print i valuesfor (j = 1; j <= i; j++)
Cell valueConsole.Write((i + j - 1) + " ");
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 row count and the triangle updates instantly — including the total value count.

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

Live result 5 rows · 15 values
1 
2 3 
3 4 5 
4 5 6 7 
5 6 7 8 9 

Worked Walkthrough — rows = 4

Trace each cell with the formula i + j - 1.

iValues of jPrinted row
11+1-1 = 11
22, 32 3
33, 4, 53 4 5
44 … 74 5 6 7

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

C# Programs

Three complete programs: fixed height, user input, and a compact 3-row demo. Use View Output for sample results.

Example 1 — Fixed rows = 5

Nested loops print i + j - 1 with a trailing space on each cell.

C#
using System;

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

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i grows from 1 to 5 — each pass adds one more number to the row.

2. Formula. i + j - 1 starts each row at i, then climbs by one as j increases.

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

Example 2 — User Input

Read the row count and apply the same formula for any height.

C#
using System;

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

            Console.Write("Enter 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((i + j - 1) + " ");

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

2. Same formula. Three rows end at 3 4 5.

Example 3 — Compact rows = 3

A smaller fixed demo — same formula, easier to trace by hand.

C#
using System;

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

            for (i = 1; i <= rows; i++)
            {
                for (j = 1; j <= i; j++)
                    Console.Write((i + j - 1) + " ");

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

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

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong formula

Forget the - 1

Printing i + j starts row 1 at 2. Keep i + j - 1 so the first cell is 1.

missing space

Forget the trailing space

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

WriteLine early

WriteLine inside the inner loop

That puts each number on its own line. Call WriteLine() only after the inner loop.

rows = 1

Single 1

Output is just 1 (plus a trailing space).

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

Total printed values are 1 + 2 + … + n = n(n+1)/2, which is quadratic in n.

Key Takeaways

  • Rule: print i + j - 1 with a trailing space on each cell.
  • Shape: row i prints exactly i numbers and starts with i.
  • Write vs WriteLine: numbers stay on the line; WriteLine advances after each row.
  • Next step: Program 34 prints a similar triangle starting from 0.

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

Frequently Asked Questions

Because when j = 1, the expression i + j - 1 becomes i. Row 4 therefore starts with 4.
j increases by 1, so i + j - 1 increases by 1 as well — producing consecutive numbers on each row.
Program 32 uses 9 + i + j (starts at 11). Program 33 uses i + j - 1 (starts at 1).
Program 33 uses i + j - 1 with i starting at 1. Program 34 uses i + j with i starting at 0.
Console.Write((i + j - 1) + " ") keeps values separated on the same row. WriteLine ends the row.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.
Prefer int.TryParse(Console.ReadLine(), out rows) so bad input does not throw FormatException.
Only one row prints — a single 1.

Did you know?

Each printed value is computed as i + j - 1. Row i = 1 prints 1; row i = 4 prints 4, 5, 6, 7 — a left-shifted increasing triangle starting at 1.

Next: Increasing Triangle from 0

Same triangle shape, but the formula starts at 0 instead of 1.

Program 34 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