Right-Aligned Descending Number Triangle in C#

What You’ll Learn
How to print a right-aligned triangle where each row prints numbers in descending order: 1, then 21, then 321, and so on.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
Output
1
21
321
4321
543211
Complete C# Program
This matches the reference logic: loop j from 5 down to 1, printing spaces while j > i, otherwise print j.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
for (i = 1; i <= 5; i++)
{
for (j = 5; j >= 1; j--)
{
if (j > i)
Console.Write(" ");
else
Console.Write(j);
}
Console.WriteLine();
}
}
}
}2
Variation — User Input rows
Make the number of rows configurable:
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter rows: ");
int rows = Convert.ToInt32(Console.ReadLine());
if (rows < 1) return;
for (int i = 1; i <= rows; i++)
{
for (int j = rows; j >= 1; j--)
Console.Write((j > i) ? " " : j.ToString());
Console.WriteLine();
}
}
}
}12 people found this page helpful
