Right-Aligned Decreasing Number Triangle in C#

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

What You’ll Learn

How to print a right-aligned triangle where each row shows a decreasing sequence starting from rows (5 in this example).

You’ll use one loop for indentation (spaces) and another loop to print the numbers rows down to the current row limit.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
        5
      5 4
    5 4 3
  5 4 3 2
5 4 3 2 1
1

Complete C# Program

The first inner loop prints indentation spaces. The second inner loop prints the decreasing numbers from rows down to i.

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 = 1; j < i; j++)
                    Console.Write("  ");

                for (j = rows; j >= i; j--)
                    Console.Write("{0,2}", j);

                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Set the number of rows

int rows = 5; controls both the height and the starting number for each row.

Setup
2

Outer loop (top to bottom)

for (i = rows; i >= 1; i--) decides how many numbers are printed on each row.

Row control
3

Indentation loop

for (j = 1; j < i; j++) prints two spaces per step, so higher rows shift to the right.

Alignment
4

Number loop (decreasing sequence)

for (j = rows; j >= i; j--) prints rows, rows-1, ..., down to i for the current row.

Printing
=

Right-aligned decreasing triangle

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

2

Variation — User Input Rows

Read the size at runtime. This version starts each row from rows automatically.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            int i, j;

            Console.Write("Enter number of rows: ");
            if (!int.TryParse(Console.ReadLine(), out rows) || rows <= 0)
            {
                Console.WriteLine("Please enter a positive integer.");
                return;
            }

            for (i = rows; i >= 1; i--)
            {
                for (j = 1; j < i; j++)
                    Console.Write("  ");

                for (j = rows; j >= i; j--)
                    Console.Write("{0,2}", j);

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Change the indentation from " " to " " for wider spacing
  • Increase the width from {0,2} to {0,3} for larger row counts
  • Start from a custom maximum (e.g., 9) by using max instead of rows
  • Print an ascending sequence by looping j upward

Avoid

  • Mixing different indent widths across rows (it breaks alignment)
  • Forgetting Console.WriteLine() after each row
  • Using a very small width when numbers become two digits

Key Takeaways

1

Two inner loops make the pattern easy: one for spaces, one for numbers.

2

The decreasing sequence comes from for (j = rows; j >= i; j--).

3

{0,2} keeps console output aligned (use a bigger width for larger values).

4

Total prints are triangular numbers: n(n+1)/2 \(\Rightarrow\) O(n²).

❓ Frequently Asked Questions

Because the second inner loop starts from rows (5 here) and counts down to i.
Remove the indentation loop that prints spaces. Then the numbers will start at the left margin.
Yes. Replace rows with a separate max variable and use it as the starting value in the number loop.
The output still works, but you may want a larger format width (for example {0,3} or {0,4}) so multi-digit values stay aligned.

Explore More C# Number Patterns!

Keep practicing with aligned triangles and sequence-based patterns.

All Number Patterns →
Did you know?

Console alignment becomes much easier when you print each number with a fixed width (like {0,2}). It keeps columns stable as the shape grows.

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