Reverse Row Number Triangle in C#

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

Program 7 prints a reverse row number triangle: each row shows digits from the current row index down to 11, 21, 321, and so on. This tutorial covers the shape rule, ascending outer loop, inner countdown i..1, a live preview, worked C# examples, edge cases, and complexity.

Shape Rule

i..1 per row

Row with outer i = 1 prints 1; row with i = 5 prints 54321 — digits grow from the right.

Outer Loop

i = 1..rows

for (i = 1; i <= rows; i++) — row width increases from one digit to rows digits.

Inner Loop

j = i..1

for (j = i; j >= 1; j--) prints digits in reverse order on each row.

Write vs WriteLine

Same line / next line

Digits use Console.Write(j); end each row with WriteLine().

Live Preview

rows = 3..9

Pick row count and draw the reverse row triangle in the browser.

O(n²)

Complexity

Total prints = 1+2+…+n = n(n+1)/2 — a triangular number.

Introduction

A reverse row number triangle grows digits from the right: each row prints numbers from the current row index down to 1. With rows = 5, you get 1, 21, 321, 4321, 54321.

In C# use an outer loop counting up from 1 to rows, an inner loop printing j from i down to 1, then Console.WriteLine() after each row.

Why it matters?

It pairs with Program 6’s left-growing triangle — the inner loop counts down instead of up, teaching reverse iteration.

Key Highlights

Outer up

i = 1..rows — narrow row first.

Inner i..1

Countdown from i to 1.

vs Program 6

Program 6 outer down, inner i..rows; Program 7 outer up, inner i..1.

Series Step

Follow Program 6; continue to Program 8 next.

In short: outer i = 1..rows, inner j = i..1, Write(j) per digit, then WriteLine().

📝 Problem & Approach

Given row count rows = 5, print a reverse row number triangle — row outer index i shows digits i..1.

C#
// rows = 5
//1
//21
//321
//4321
//54321

Inputs & Outputs

ItemTypeDescription
rowsintTriangle height — also the widest row digit count.
i (outer)intCurrent row index — runs 1 up to rows.
j (inner)intPrints i..1 with Console.Write(j).
Row widthintRow with outer i prints exactly i digits.
First rowintSingle digit 1 when i = 1.
Last rowstringDigits rows..1 when i = rows.

Minimal workflow

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

Approach comparison

ApproachIdeaBest for
Ascending outerfor (i = 1; i <= rows; i++)Narrow-first row order
Inner i..1Countdown from i to 1Right-growing triangle
User-input rowsint.TryParse(...)Flexible height
Compact tracerows = 3 on paper firstQuick dry-runs
Spaced outputConsole.Write(j + " ")Readable columns

⚡ Quick Reference

GoalPattern
Outer loopfor (i = 1; i <= rows; i++)
Inner loopfor (j = i; j >= 1; j--) Console.Write(j);
End rowConsole.WriteLine();
Program 6 contrastProgram 6: outer down, inner i..rows; Program 7: outer up, inner i..1

📋 Fixed Rows vs User Input vs Compact Trace

Same reverse row triangle — three ways to set row count and trace the logic.

Fixed rows
rows = 5

Hard-coded height for demos

User input
TryParse

Read row count from console

Compact trace
rows = 3

Quick dry-run on paper

Outer
i = 1..rows

Ascending row index

Inner
j = i..1

Countdown per row

Context

When This Pattern Shows Up

Reach for this pattern when teaching ascending outer loops, inner countdown, and comparing shapes with Program 6.

  1. Post Program 6 exercise

    Natural companion to Program 6 — same triangular print count, inner loop counts down instead of up.

  2. Reverse counting drills

    Inner loop j-- from i to 1 — essential countdown practice.

  3. Interview warm-ups

    Classic nested-loop question — explain outer up, inner countdown before coding.

  4. Gateway to Program 8

    Compare this right-growing triangle with the next pattern in the series.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in ascending outers, inner countdown, and O(n²) thinking.

🔮 Live Preview

Choose a row count between 3 and 9 and draw the reverse row number triangle in the browser.

Try 4, 5, or 7. Max up to 9 in this preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C# programs — fixed rows, user input, and a compact trace with rows = 3. Click View Output to reveal sample console results.

📚 Getting Started

Print five rows of the reverse row number triangle with nested loops.

Example 1 — Fixed rows = 5

Hard-coded height — outer loop up, inner loop counts down each row.

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 = i; j >= 1; j--)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }
        }
    }
}

How It Works

Outer i runs 1 to 5 — inner j prints i down to 1 on each row.

📈 Practical Variant

Read row count from the user with validation.

Example 2 — User Input Rows

Configurable height with int.TryParse and a positive-rows check.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            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

Same nested loops — only the row count comes from console input with safe parsing.

⚡ Compact Trace

Use rows = 3 for a quick paper trace before larger triangles.

Example 3 — Compact rows = 3 Trace

Small triangle — easy to dry-run on paper before scaling up.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            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

Three rows, six total digits — trace i and j on paper before coding rows = 5.

🧠 How the Nested Loops Build Each Row

1

Choose the row count

int rows = 5; sets how many rows to print.

Setup
2

Outer loop (row index)

for (i = 1; i <= rows; i++) moves from row 1 to row 5.

Row control
3

Inner loop (print i..1)

for (j = i; j >= 1; j--) prints digits in reverse order for each row.

Reverse print
4

New line

Console.WriteLine() moves to the next row after each line is printed.

Line break
=

Reverse row triangle complete

Total printed digits follow triangular numbers: n(n+1)/2, so time complexity is O(n²).

🔎 Worked Walkthrough — rows = 5

Trace each row — outer i sets width, inner j counts down from i to 1.

Row (i)Inner j valuesOutput line
111
22, 121
33, 2, 1321
44, 3, 2, 14321
55, 4, 3, 2, 154321

Total digits printed: 1+2+3+4+5 = 15 = 5×6/2 — the fifth triangular number.

Use Cases

Where this tiny pattern (and its countdown inner loop) shows up beyond the homework prompt.

1. Teaching Countdown Loops

Inner j-- from i to 1 — concrete reverse iteration practice.

Example: trace row 3 and watch j print 3, 2, 1.

2. Pair with Program 6

Program 6 grows digits from the left; Program 7 grows from the right — same O(n²) total.

Example: print both patterns side by side for rows = 5.

3. Console Formatting Drills

Practice Write vs WriteLine without complex math.

Example: put WriteLine inside the inner loop by mistake.

4. Character Substitution

Swap digits for letters, stars, or spaced output once the loop works.

Example: print j + " " for spaced digits on each row.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: count printed digits for n = 10 → 55.

6. Input Validation Labs

Pair the pattern with TryParse and positive-row checks.

Example: reject rows <= 0 and re-prompt.

Pro Tip: when an interviewer asks for patterns, explain outer up and inner countdown first — then write the loops.

Advantages

Why this pattern earns a permanent spot in beginner C# courses.

  1. 1. Instant Visual Feedback

    Using j++ instead of j-- shows up immediately as wrong row order.

  2. 2. Minimal Concepts

    Only loops and console output — no arrays or math libraries.

  3. 3. Easy to Extend

    Add spaces, right-align, or swap digits for stars with small edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters.

Pro Tip: trace i and j on paper for rows = 3 before coding — watch how each row adds one digit on the right.

Usage Tips

Small habits that keep reverse-row triangle code clean.

  1. 1. Name Bounds Clearly

    Use rows (or n) and keep i/j for row/column loops.

  2. 2. Prefer TryParse

    Avoid crashes when the user types letters instead of a number.

  3. 3. Keep WriteLine Outside

    Only call WriteLine() after the inner loop finishes the row.

  4. 4. Count Down on the Inner Loop

    for (j = i; j >= 1; j--) matches “row i prints digits i..1” naturally.

  5. 5. Dry-Run One Small n

    Trace rows = 3 on paper before coding larger demos.

Pro Tip: if rows print ascending digits (12, 123, 1234), you used j++ instead of j--.

Common Pitfalls

Mistakes that commonly break reverse row number triangles.

  1. 1. Incrementing j Instead of Decrementing

    j++ prints ascending digits per row — 12, 123, 1234 instead of 21, 321, 4321.

    → Use for (j = i; j >= 1; j--).

  2. 2. Forgetting WriteLine After Each Row

    All digits print on one long line without a row break.

    → Call Console.WriteLine() after the inner loop.

  3. 3. Zero or Negative Rows

    Invalid input may print nothing or behave unexpectedly.

    → Validate rows > 0 before the loops.

  4. 4. Blind Convert.ToInt32

    Letters or empty input throw FormatException.

    → Prefer TryParse and re-prompt on failure.

  5. 5. WriteLine Inside Inner Loop

    Each digit prints on its own line — vertical output instead of a triangle.

    → Use Write(j) inside, WriteLine() outside only.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single row

Prints only 1 — inner loop runs once with j = 1.

rows = 0

Zero rows

Outer loop never runs — print nothing or show a message.

rows = 2

Minimal triangle

Output 1 then 21 — good quick test.

Negative

Negative rows

Reject with validation — outer loop condition fails silently otherwise.

Bad input

Non-numeric ReadLine

Convert.ToInt32 throws — use TryParse.

Large n

Many rows

Still O(n²) prints — cap rows for console demos.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Compare with Program 6

  • Program 6: outer down, inner i..rows
  • Program 7: outer up, inner i..1

2. Add spaces between digits

  • Use Console.Write(j + " ")
  • Same loops, readable columns

3. Next in series

  • Continue with Program 8
  • Next number pattern in the series

4. Paper trace

  • Dry-run rows = 3 before coding
  • Fill the walkthrough table by hand

Notes

  • Row width. Row i prints exactly i digits — the triangle widens from the right.
  • Total digits = n(n+1)/2 — a triangular number. For rows = 5, that is 15 digits.
  • Program 5 prints 1..i ascending; Program 7 prints i..1 descending — mirror per-row logic.
  • The last row always shows digits from rows down to 1 — e.g. 54321 when rows = 5.

Quick Takeaway: outer i = 1..rows, inner j = i..1, Write(j), then WriteLine().

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
Compact trace (Example 3)O(rows²)O(1)
Wrap Up

🎉 Conclusion

The reverse row number triangle is a compact nested-loop exercise: outer counts up, inner counts down, and each row grows one digit wider from the right. Master the fixed rows = 5 version, then try user input with TryParse and the compact rows = 3 trace.

Practice the three examples above, then continue to Program 8 for the next pattern in the series.

Use j-- for reverse digits per row, keep WriteLine outside the inner loop, and validate row count when reading from the console.

💡 Best Practices

✅ Do

  • Explain outer up and inner countdown before coding
  • Use for (j = i; j >= 1; j--)
  • Call WriteLine() after each inner loop
  • Validate rows > 0 for user input
  • Dry-run rows = 3 on paper first
  • State O(n²) time when asked about complexity

❌ Don’t

  • Use j++ when the pattern needs countdown
  • Put WriteLine inside the inner loop
  • Skip input validation on console reads
  • Confuse this with Program 6’s left-growing triangle
  • Skip the rows = 3 dry-run before larger demos

Key Takeaways

Knowledge Unlocked

Five things to remember about this reverse row pattern

Print the reverse row number triangle the beginner-friendly way.

5
Core concepts
02

Outer

i = 1..rows

Loop
03

Inner

j = i..1 countdown

Loop
W 04

Output

Write then WriteLine

Console
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

It prints 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. That prints i, i-1, ..., 1 on each row.
When i = rows (5), the inner loop prints j from 5 down to 1 — giving 54321 on the last line.
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 — digits grow from the right instead of the left.
Console.Write stays on the same line for each digit. Console.WriteLine ends the row after the inner loop finishes.
Change rows or read it from user input with TryParse — see Example 2.
Yes — use an ascending inner loop for (j = 1; j <= i; j++) like Program 5.
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.

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²).

Continue to Program 8

Move on to the next pattern in the C# number-pattern series.

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.

11 people found this page helpful