Right-Aligned Incremental 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 numbers increase continuously: 1, then 2 3, then 4 5 6, and so on.

You’ll practice two key ideas: nested loops (row/column control) and formatted output (fixed-width alignment using {0,3}).

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
             1
          2  3
       4  5  6
    7  8  9 10
11 12 13 14 15
1

Complete C# Program

We print spaces until the diagonal, then print the next number k with fixed-width formatting so columns stay aligned.

C#
using System;

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

            for (i = 1; i <= rows; i++)
            {
                for (j = rows; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write("   ");
                    else
                        Console.Write("{0,3}", k++);
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Initialize rows and counter

rows controls the height, and k starts at 1 and increases every time we print a number.

Setup
2

Outer loop (rows)

for (i = 1; i <= rows; i++) prints one line per row.

Row control
3

Inner loop (alignment + numbers)

The loop for (j = rows; j >= 1; j--) prints indentation while j > i, then prints numbers when j <= i.

Formatting
4

Fixed-width printing

Console.Write("{0,3}", k++) reserves 3 columns per number so 10, 11, etc. don’t shift the triangle.

Aligned output
=

Right-aligned incremental triangle

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

2

Variation — User Input Rows

Read the row count at runtime. The pattern logic stays the same.

C#
using System;

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

            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 = 1; i <= rows; i++)
            {
                for (j = rows; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write("   ");
                    else
                        Console.Write("{0,3}", k++);
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Increase the width from {0,3} to {0,4} for larger row counts
  • Print left-aligned by removing the indentation branch
  • Reset k each row to make 1, then 1 2, then 1 2 3
  • Replace numbers with characters to create alphabet triangles

Avoid

  • Using inconsistent spacing (it will misalign once numbers reach two digits)
  • Not validating input when using user-entered rows
  • Forgetting Console.WriteLine() at the end of each row

Key Takeaways

1

A counter k creates a continuous sequence across rows.

2

Right alignment comes from printing blanks while j > i.

3

{0,3} keeps columns aligned when values become two digits.

4

Work grows as O(n²) because total prints are n(n+1)/2.

❓ Frequently Asked Questions

Because k is declared outside the loops and is incremented each time a number is printed, so it continues across rows.
Remove the indentation branch that prints " ". Then print numbers directly with an inner loop from 1 to i.
It makes it easy to decide whether to print spaces or a number by comparing j with the current row i.
The total is 1+2+…+n = n(n+1)/2.

Explore More C# Number Patterns!

Practice nested loops with triangles, pyramids, and incremental patterns.

All Number Patterns →
Did you know?

Using {0,3} (or any fixed width) is a simple trick to keep console patterns aligned as values grow from single digits to double digits and beyond.

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