C# Ascending Number Triangle Pattern

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

What Is This Pattern?

An ascending number triangle prints digits 1 through i on row i, growing one digit per line.

Remember
Rule: on row i, print 1..i with Console.Write

1
12
123
1234
12345   ← rows = 5

In C# the outer loop runs i from 1 to rows; the inner loop prints j from 1 to i, then WriteLine ends the row.

How to Solve It

One nested loop: grow the row count, print 1 through i each time.

MethodIdeaBest for
Tight digitsConsole.Write(j) for j = 1..iLearning, interviews, exams
Spaced digitsConsole.Write(j + " ")Easier reading on screen

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print j
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (i = 1; i <= rows; i++)
Print 1..ifor (j = 1; j <= i; j++) Console.Write(j);
Spaced digitsConsole.Write(j + " ");
End the rowConsole.WriteLine();

Write vs WriteLine

APIEffectUse for
Console.WriteStays on the same lineEach digit on the row
Console.WriteLineEnds the current lineAfter the inner loop finishes a row

Live Preview

Change the row count and the ascending triangle updates instantly.

Whole numbers from 1 to 9 (keeps single-digit rows). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 digits
1
12
123
1234
12345

Worked Walkthrough — rows = 4

Trace how the inner bound grows with i.

iInner jPrinted row
111
21..212
31..3123
41..41234

Total digits for n rows = n(n+1)/2. Each row is one digit longer than the previous.

C# Programs

Three complete programs: fixed 5 rows, user-input rows, and spaced digits. Use View Output for sample results.

Example 1 — Fixed rows = 5

Nested loops print digits tightly with Console.Write(j).

C#
using System;

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

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i runs from 1 to 5 — one row per value.

2. Inner loop. Print j from 1 to i so the row length matches i.

3. New line. WriteLine after the inner loop starts the next row.

Example 2 — User Input (rows)

Read the row count and build the same growing triangle.

C#
using System;

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

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

2. Same triangle. Only the outer bound changes from the literal 5.

Example 3 — Spaced Digits

Add a space after each digit for easier reading.

C#
using System;

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

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

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same loops. Bounds stay i = 1..rows and j = 1..i.

2. Only the print changes. j + " " separates digits without changing the triangle shape.

Edge Cases & Pitfalls

Check these before calling the solution done.

i--

Outer loop counts down

You get Program 1’s shrinking triangle (12345, 1234, …). Keep i from 1 up to rows.

WriteLine

WriteLine inside the inner loop

That puts every digit on its own line. Call WriteLine only after the column loop.

print i

Print i instead of j

You get 1, 22, 333, 4444… Print the column index j for 1..i.

rows = 1

Single row

Output is just 1 — one digit on one line.

rows > 9

Multi-digit values

Printing 10+ without spaces runs digits together. Cap at 9 or use spaces / fixed width.

Bad input

Convert.ToInt32 throws

Prefer int.TryParse so non-numeric input does not crash the program.

Time and Space Complexity

ProgramTimeExtra space
Fixed / spaced (Examples 1, 3)O(n²)O(1)
User input (Example 2)O(n²)O(1)

Total printed digits are 1 + 2 + … + n = n(n+1)/2, so work is quadratic in the row count.

Key Takeaways

  • Rule: on row i, print digits 1..i — the triangle grows upward.
  • Loops: outer i = 1..rows, inner j = 1..i.
  • Write vs WriteLine: digits stay on the line; WriteLine advances after each row.
  • Next step: Program 6 prints a reverse ascending triangle (5, 45, 345…).

One line: for each row i from 1 to rows, print 1 through i.

Frequently Asked Questions

The outer loop runs i from 1 to rows. For each row i, the inner loop runs j from 1 to i and prints j. Row 1 prints 1, row 2 prints 12, and so on.
The outer loop counts up and the inner bound equals i. When i increases, each row prints one more digit than the previous row.
Console.Write stays on the same line. Console.WriteLine ends the current line. Digits use Write; the row break uses WriteLine after the inner loop.
Program 4 prints rows..i in descending order (54321, 5432, …). Program 5 prints 1..i in ascending order — a growing triangle instead of a shrinking one.
Replace 5 with rows in the outer loop bound — see Example 2.
Count the outer loop down: for (i = rows; i >= 1; i--). Keep the inner loop as for (j = 1; j <= i; j++) — that is Program 1.
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 digit 1 on one line.

Did you know?

Row i prints digits 1 through i. The outer loop counts up from 1 to rows, so each row grows by one digit — total prints = n(n+1)/2.

Next: Reverse Ascending Number Triangle

Print 5, 45, 345, 2345, 12345 with outer loop counting down.

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