Reverse Descending Number Triangle in C#

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:
54321
4321
321
21
1Complete C# Program
The outer loop controls the row length. The inner loop prints from i down to 1.
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
Set the number of rows
int rows = 5; fixes the height of the triangle.
Outer loop (row limit)
for (i = rows; i >= 1; i--) decreases the row limit from 5 down to 1.
Inner loop (print i..1)
for (j = i; j >= 1; j--) prints numbers in reverse order on each row: i, i-1, ..., 1.
New line
Console.WriteLine() moves to the next row after each inner loop finishes.
Reverse descending triangle
Total prints are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Read the number of rows using Console.ReadLine():
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.TryParsebefore converting - Add spaces between digits for readability
- Change the inner loop to print
1..ifor an ascending row order - Right-align the triangle by printing leading spaces
Avoid
- Forgetting
Console.WriteLine()after each row - Using
Convert.ToInt32without handling invalid input - Incrementing
jin the inner loop (it must count down)
Key Takeaways
The outer loop reduces the row limit from rows down to 1.
The inner loop prints in reverse order from i down to 1.
Total output size is triangular: n(n+1)/2, so complexity is O(n²).
These loop bounds are useful for other reverse patterns too.
❓ Frequently Asked Questions
rows = 5 and the outer loop starts with i = rows. That makes the first row print from 5 down to 1.for (j = 1; j <= i; j++).n(n+1)/2.Explore More C# Number Patterns!
Keep practicing loop bounds with inversions, alignments, and alternating row patterns.
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.
12 people found this page helpful
