Reverse Row Number Triangle in C#

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:
1
21
321
4321
54321Complete C# Program
The outer loop increases from 1 to rows. 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 = 1; i <= rows; i++)
{
for (j = i; j >= 1; j--)
{
Console.Write(j);
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Choose the row count
int rows = 5; sets how many rows to print.
Outer loop (row index)
for (i = 1; i <= rows; i++) moves from row 1 to row 5.
Inner loop (print i..1)
for (j = i; j >= 1; j--) prints digits in reverse order for each row.
New line
Console.WriteLine() moves to the next row after each line is printed.
Reverse row triangle
Total printed digits follow triangular numbers: n(n+1)/2, so time complexity is O(n²).
Variation — User Input Version
Read the row count at runtime:
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.TryParseto avoid crashes - Add spaces between digits with
Console.Write(j + " ") - Print the normal order
1..ito 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
The outer loop chooses the row number (i).
The inner loop prints from i down to 1.
Total output size is triangular: n(n+1)/2.
This pattern reinforces reverse counting in nested loops.
❓ Frequently Asked Questions
i = rows (5). The inner loop then prints j from 5 down to 1.1 to i using for (j = 1; j <= i; j++) (see Program 1).n(n+1)/2.Explore More C# Number Patterns!
Practice reverse counting, loop bounds, and alignments to master pattern printing.
The total number of printed digits is a triangular number. For n rows, it’s n(n+1)/2.
12 people found this page helpful
