Right-Aligned Descending Number Triangle in C#

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Condition + Loops

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
54321
1

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();
            }
        }
    }
}

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