Reverse Row Number Triangle in C#

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a reverse row number triangle in C#. Each row prints numbers from the current row index down to 1: 1, 21, 321, 4321, and so on.

This is a helpful pattern for practicing a nested loop where the inner loop counts down.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
21
321
4321
54321
1

Complete C# Program

The outer loop increases from 1 to rows. The inner loop prints from i down to 1.

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

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

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

2

Variation — User Input Version

Read the row count at runtime:

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the number of rows: ");
            int rows = Convert.ToInt32(Console.ReadLine());

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

💡 Tips for Enhancement

Try These

  • Validate input with int.TryParse to avoid crashes
  • Add spaces between digits with Console.Write(j + " ")
  • Print the normal order 1..i to get Program 5
  • Right-align the triangle by printing leading spaces

Avoid

  • Forgetting Console.WriteLine() after each row
  • Using negative or zero rows without validating input
  • Incrementing j (this pattern needs a countdown)

Key Takeaways

1

The outer loop chooses the row number (i).

2

The inner loop prints from i down to 1.

3

Total output size is triangular: n(n+1)/2.

4

This pattern reinforces reverse counting in nested loops.

❓ Frequently Asked Questions

On the last iteration, i = rows (5). The inner loop then prints j from 5 down to 1.
Yes. Keep the outer loop descending and print from 1 to i using for (j = 1; j <= i; j++) (see Program 1).
O(n²) for n rows since total prints are n(n+1)/2.

Explore More C# Number Patterns!

Practice reverse counting, loop bounds, and alignments to master pattern printing.

All Number Patterns →
Did you know?

The total number of printed digits is a triangular number. For n rows, it’s n(n+1)/2.

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