C# Reverse Number Triangle Pattern (Growing)

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

What Is This Pattern?

A reverse row number triangle grows one digit per row, but each row prints digits downward from the row index to 1 — 1, 21, 321, and so on.

Remember
Rule: for i from 1 to rows
        for j from i down to 1: print j (no space)

1
21
321
4321
54321     ← rows = 5

Follows the reverse ascending triangle in Program 6; next is the reverse left-growing triangle in Program 8.

How to Solve It

Outer loop counts up from 1 to rows. Inner loop counts down from i to 1. End each row with Console.WriteLine().

MethodIdeaBest for
Ascending outer + descending innerOuter i = 1..rows; inner j = i..1; print jLearning, interviews, exams
Spaced digitsConsole.Write(j + " ") instead of Console.Write(j)Easier reading on each row

Pseudocode

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

Cheat sheet

GoalPattern
Pick each rowfor (int i = 1; i <= rows; i++)
Print digitsfor (int j = i; j >= 1; j--) Console.Write(j);
End of rowConsole.WriteLine();
Spaced formConsole.Write(j + " ");
vs Program 5 / 65: 1..i; 6: i..rows; 7: i..1

Write vs WriteLine

APIEffectUse for
Console.Write(j)Stays on the same lineEach digit on the row
Console.WriteLine()Ends the current lineAfter the inner loop finishes

Print digits with Write (no newline), then call WriteLine() once per row.

Live Preview

Change the row count and the reverse-row triangle updates instantly — capped at 9 for readable single-digit demos.

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

Live result rows = 5 · 15 digits
1
21
321
4321
54321

Worked Walkthrough — rows = 5

Trace how a descending inner loop reverses each row while the outer loop grows the width.

iInner jPrints
11..11
22..121
33..1321
55..154321

Total digits = n(n+1)/2 → O(n²). Compare with Program 5 (1..i) and Program 6 (i..rows).

C# Programs

Three complete programs: fixed rows = 5, TryParse input, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — outer counts up, inner prints i..1 with no spaces.

C#
using System;

class Program
{
    static void Main()
    {
        int rows = 5;

        for (int i = 1; i <= rows; i++)
        {
            for (int j = i; j >= 1; j--)
                Console.Write(j);
            Console.WriteLine();
        }
    }
}

How It Works

1. Outer up. i grows from 1 to 5, so each row is one digit wider.

2. Inner down. j starts at i and decrements to 1 — that reverses the digits.

3. Newline. Call Console.WriteLine() only after the inner loop finishes the row.

Example 2 — User Input Rows

Read rows with int.TryParse, validate, then run the same nested loops.

C#
using System;

class Program
{
    static void Main()
    {
        Console.Write("Enter the number of rows: ");
        if (!int.TryParse(Console.ReadLine(), out int rows) || rows <= 0)
        {
            Console.WriteLine("Please enter a positive integer.");
            return;
        }

        for (int i = 1; i <= rows; i++)
        {
            for (int j = i; j >= 1; j--)
                Console.Write(j);
            Console.WriteLine();
        }
    }
}

How It Works

1. Prompt and validate. Prefer TryParse so bad input does not throw. Reject non-positive values.

2. Same core. Outer up / inner i..1 matches Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable single-digit output:

Safer input
if (!int.TryParse(Console.ReadLine(), out int rows) || rows < 1 || rows > 9)
{
    Console.WriteLine("Enter a whole number from 1 to 9.");
    return;
}

Example 3 — Compact rows = 3

Same structure with only three rows — easy to trace on paper.

C#
using System;

class Program
{
    static void Main()
    {
        int rows = 3;

        for (int i = 1; i <= rows; i++)
        {
            for (int j = i; j >= 1; j--)
                Console.Write(j);
            Console.WriteLine();
        }
    }
}

How It Works

1. Three rows. i = 1 → 1; i = 2 → 21; i = 3 → 321.

2. Trace on paper. Confirm each new digit is prepended on the left of the descending sequence.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

j = 1..i

Got Program 5 instead

If the inner loop counts up from 1 to i, you get 1, 12, 123… Keep j = i; j >= 1; j--.

i = rows..1

Wrong outer direction

A descending outer with inner i..rows is Program 6. Program 7 needs outer 1..rows and inner i..1.

WriteLine inside

Broken rows

If WriteLine() sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.

rows = 1

Single digit

Output is just 1 — a good sanity check for input validation.

rows > 9

Multi-digit width

Digits 10+ make tight rows hard to read. Cap demos at 9 or switch to spaced Write(j + " ").

TryParse

Validate input

Prefer int.TryParse and require rows > 0 before looping — bad input should not throw.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Total digits printed = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Grow then reverse: outer 1..rows, inner i..1.
  • Start at i: the first digit on each row is the row index itself.
  • Break the row: Console.Write(j) in the inner loop; WriteLine() after it.
  • Complexity: O(n²) time from n(n+1)/2 digits; O(1) extra space.

One line: count i up to rows, print j from i down to 1, then newline.

Frequently Asked Questions

A reverse row number triangle: row 1 shows 1, row 2 shows 21, row 3 shows 321, and so on until the last row shows digits from rows down to 1.
The inner loop starts at j = i and decrements to 1 — printing i, i-1, ..., 1 on each row.
When i = rows (5), the inner loop prints j from 5 down to 1.
Program 6 outer counts down and prints i..rows (5, 45, 345). Program 7 outer counts up and prints i down to 1 (1, 21, 321).
Program 5 prints 1..i ascending per row. Program 7 prints i..1 descending per row.
Console.Write(j) stays on the same line. Console.WriteLine() ends the row after the inner loop.
Change rows or read it from user input with int.TryParse — see Example 2.
O(n²) for n rows because total prints are 1 + 2 + ... + n = n(n+1)/2.

Did you know?

Each row prints digits in reverse order — outer i runs 1..rows, inner j counts down from i to 1 — producing 1, 21, 321, and so on. Total prints grow as O(n²).

Next: Reverse Left-Growing Triangle

Continue with rows like 5, 54, 543 that count down from the peak.

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

12 people found this page helpful