Right-Aligned Sequential Pyramid in C#

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

What You’ll Learn

This pattern prints letters in a running sequence (A, then B C, then D E F…) but keeps the triangle right-aligned by printing empty cells first.

For best alignment, view output in a monospace terminal because we rely on fixed-width cells.

⭐ Pattern Output

Output for 5 rows:

Output
        A
      B C
    D E F
  G H I J
K L M N O
1

Complete C# Program (Fixed 5 Rows)

Direct adaptation of the reference logic: a single counter k increments only when a letter is printed, and {0,2} keeps columns aligned.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            char k = 'A';

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

🧠 How It Works

1

k = 'A'

A single running counter that never resets between rows.

Counter
2

Right alignment via empty cells

The inner scan runs from 5 down to 1. When j > i we print two spaces (" ") to keep the same cell width as a letter.

Align
3

Fixed-width letter printing

We print letters using {0,2}, so each letter occupies 2 columns and lines up with the padding.

Columns
=

Sequence continues

Because k increments only when we print a letter, the alphabet continues across rows.

2

Variation — User Input Rows

Choose how many rows to print. Note: for large values, the letters will go past Z.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter number of rows (like 5): ");
            int n = int.Parse(Console.ReadLine());

            char k = 'A';
            for (int i = 1; i <= n; i++)
            {
                for (int j = n; j >= 1; j--)
                {
                    if (j > i)
                        Console.Write("  ");
                    else
                        Console.Write("{0,2}", k++);
                }
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Print a space after each letter to get a more airy pyramid
  • Reset k every row if you want rows like A, A B, A B C
  • Use lowercase by starting at 'a'
  • Center the whole shape by adding leading padding

Avoid

  • Viewing output in proportional fonts (alignment will look off)
  • Printing one space for padding (cells become uneven)
  • Letting k run past Z without a wrap rule

Key Takeaways

1

k is a running sequence across rows.

2

Two spaces match the width of {0,2}.

3

Right alignment comes from printing empty cells first.

4

Total work is O(n²) for n rows.

❓ Frequently Asked Questions

Because the letters are printed using a 2-character field ({0,2}). Two spaces keeps empty cells aligned with printed cells.
Decide a rule: stop at Z, wrap back to A, or switch to a longer alphabet list. Then apply it when incrementing k.
O(n²) for n rows because you print a triangular number of cells.

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