C# Incremental Number Triangle Pattern (Right-Aligned)

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

What Is This Pattern?

A right-aligned incremental triangle pads each row with spaces, then prints the next values from a continuous counter k that never resets between rows.

Remember
Rule: spaces while j > i; else print k++ in width 3

             1
          2  3
       4  5  6
    7  8  9 10
 11 12 13 14 15   ← 5 rows

In C# the inner loop always runs from rows down to 1. Print " " when j > i; otherwise Console.Write("{0,3}", k++).

How to Solve It

One fixed-width loop with a shared counter and format width.

MethodIdeaBest for
Counter + spacesPad left; print k++ with {0,3}Learning, interviews, exams
User-input rowsSame logic with a variable heightPractice / demos

Pseudocode

Pseudocode
k = 1
for i from 1 to rows:
    for j from rows down to 1:
        if j > i: print three spaces
        else: print k (width 3), then k = k + 1
    print newline

Cheat sheet

GoalPattern
Grow filled widthfor (i = 1; i <= rows; i++)
Fixed column scanfor (j = rows; j >= 1; j--)
Leading padif (j > i) Console.Write(" ");
Next numberConsole.Write("{0,3}", k++);
End the rowConsole.WriteLine();

Write vs WriteLine

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

Live Preview

Change the row count and the right-aligned counter triangle updates instantly.

Whole numbers from 1 to 8 (width-3 columns stay readable). Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 15 values
             1
          2  3
       4  5  6
    7  8  9 10
 11 12 13 14 15

Worked Walkthrough — rows = 4

Trace spaces vs numbers. Counter k keeps climbing across rows.

iSpaces / numbersPrinted row
13 pads / 11
22 pads / 2 32 3
31 pad / 4 5 64 5 6
4none / 7..107 8 9 10

Each row is always 3 × rows characters wide — space groups and formatted numbers combined.

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

Fixed-width scan: print three spaces while j > i, otherwise print and advance k.

C#
using System;

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

            for (i = 1; i <= 5; i++)
            {
                for (j = 5; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write("   ");
                    else
                        Console.Write("{0,3}", k++);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Outer loop. i grows from 1 to 5 — each pass fills one more number slot on the right.

2. Inner loop. j scans from 5 down to 1. Spaces while j > i; otherwise print k++ in width 3.

3. Continuous k. k lives outside the loops, so the sequence runs 1…15 without resetting.

Example 2 — User Input

Read the row count; the inner loop uses rows as the fixed width.

C#
using System;

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

            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 = rows; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write("   ");
                    else
                        Console.Write("{0,3}", k++);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

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

2. Same alignment. Three rows print values 1 through 6 with width-3 columns.

Example 3 — Compact rows = 3

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

C#
using System;

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

            for (i = 1; i <= rows; i++)
            {
                for (j = rows; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write("   ");
                    else
                        Console.Write("{0,3}", k++);
                }

                Console.WriteLine();
            }
        }
    }
}

How It Works

1. Same rules. Spaces while j > i; otherwise print the next k in width 3.

2. Full width. The last row has no leading pads — just 4 5 6.

Edge Cases & Pitfalls

Check these before calling the solution done.

reset k

Reset k = 1 each row

That reprints 1, 2, 3… on every row. Keep k outside both loops.

wrong pad

Print one space instead of three

Columns drift once numbers hit two digits. Match the pad width to {0,3}.

no format

Use Write(k++) without width

Alignment breaks at 10+. Prefer Console.Write("{0,3}", k++).

rows = 1

Single 1

Output is just the formatted 1 — no leading space groups.

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)

There are n rows; each runs a fixed-width inner loop of n steps, so work is n². Printed numbers alone are n(n+1)/2.

Key Takeaways

  • Rule: spaces while j > i; otherwise print continuous k++ in width 3.
  • Fixed width: the inner loop always runs rows times so columns stay right-aligned.
  • Write vs WriteLine: pads and numbers stay on the line; WriteLine advances after each row.
  • Next step: Program 36 prints a right-aligned decreasing number triangle.

One line: for each row, pad with spaces then print the next values from a shared counter using fixed column width.

Frequently Asked Questions

Numbers keep increasing across rows without resetting — row 1 prints 1, row 2 prints 2 3, row 3 prints 4 5 6, and so on.
Before printing numbers on each row, the program prints three spaces while j > i. This indents the left side so numbers shift right.
The format specifier reserves 3 columns per number (right-aligned), keeping columns aligned when values become two digits.
k is declared outside the loops and increments with k++ each time a number prints, so the sequence continues across rows.
Program 30 prints descending digits per row. Program 35 uses a continuous counter k with fixed-width formatting.
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 with no leading space groups.

Did you know?

A counter k starts at 1 and increments every time a number is printed. Leading spaces appear while j > i, and {0,3} keeps columns aligned as values grow past single digits.

Next: Right-Aligned Decreasing Triangle

Pad with spaces and print decreasing values on each row.

Program 36 tutorial →

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