Right-Aligned Number Triangle in C#

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

What You’ll Learn

How to print a right-aligned triangle where each row prints numbers from 1 to the row number.

We first print spaces, then print numbers using a fixed width so the output looks aligned.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

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

Complete C# Program

Print leading spaces to right-align, then print 1..i using {0,2} formatting.

C#
using System;

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

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

                for (k = 1; k <= i; k++)
                    Console.Write("{0,2}", k);

                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Outer loop controls rows

i runs from 1 to rows.

Rows
2

First inner loop prints spaces

The loop for (j = rows; j > i; j--) prints fewer spaces as i grows.

Indent
3

Second inner loop prints numbers

for (k = 1; k <= i; k++) prints 1..i on each row.

Numbers
4

Fixed-width formatting keeps alignment

{0,2} ensures each number takes 2 characters, so columns stay neat.

Format
2

Variation — User Input Rows

Let the user choose the number of rows at runtime.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int rows;
            Console.Write("Enter number of rows: ");
            if (!int.TryParse(Console.ReadLine(), out rows) || rows <= 0)
            {
                Console.WriteLine("Please enter a positive integer.");
                return;
            }

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

                for (int k = 1; k <= i; k++)
                    Console.Write("{0,2}", k);

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Change {0,2} to {0,3} for larger row counts
  • Print the triangle left-aligned by removing the space loop
  • Print descending numbers by looping k from i down to 1

Avoid

  • Forgetting the newline after each row
  • Using too small a field width when numbers reach two digits

Key Takeaways

1

Leading spaces shift the triangle to the right.

2

The numbers are simply 1..i for each row.

3

Fixed-width printing keeps columns aligned.

4

Total prints are triangular \(\Rightarrow\) O(n²).

Explore More C# Number Patterns!

Triangle patterns are a great starting point for mastering nested loops.

All Number Patterns →
Did you know?

Many console patterns can be built by combining an indentation loop (spaces) with a printing loop (numbers or symbols).

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