Reverse Descending 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 descending number triangle in C#. Each row prints numbers from the current row limit down to 1. For five rows, the first line is 54321, then 4321, then 321, and so on.

This is a classic nested-loop problem where the inner loop counts downward.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
54321
4321
321
21
1
1

Complete C# Program

The outer loop controls the row length. 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 = rows; i >= 1; i--)
            {
                for (j = i; j >= 1; j--)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Set the number of rows

int rows = 5; fixes the height of the triangle.

Setup
2

Outer loop (row limit)

for (i = rows; i >= 1; i--) decreases the row limit from 5 down to 1.

Row control
3

Inner loop (print i..1)

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

Reverse print
4

New line

Console.WriteLine() moves to the next row after each inner loop finishes.

Line break
=

Reverse descending triangle

Total prints are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.

2

Variation — User Input Version

Read the number of rows using Console.ReadLine():

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

💡 Tips for Enhancement

Try These

  • Validate input with int.TryParse before converting
  • Add spaces between digits for readability
  • Change the inner loop to print 1..i for an ascending row order
  • Right-align the triangle by printing leading spaces

Avoid

  • Forgetting Console.WriteLine() after each row
  • Using Convert.ToInt32 without handling invalid input
  • Incrementing j in the inner loop (it must count down)

Key Takeaways

1

The outer loop reduces the row limit from rows down to 1.

2

The inner loop prints in reverse order from i down to 1.

3

Total output size is triangular: n(n+1)/2, so complexity is O(n²).

4

These loop bounds are useful for other reverse patterns too.

❓ Frequently Asked Questions

Because rows = 5 and the outer loop starts with i = rows. That makes the first row print from 5 down to 1.
Keep the outer loop descending, but make the inner loop count up: for (j = 1; j <= i; j++).
O(n²) for n rows since total prints are n(n+1)/2.

Explore More C# Number Patterns!

Keep practicing loop bounds with inversions, alignments, and alternating row patterns.

All Number Patterns →
Did you know?

Reverse counting in the inner loop is a common technique not only for patterns, but also for traversing arrays from the end and for countdown-style algorithms.

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