Right-Aligned Alphabet Pyramid in C#

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

What You’ll Learn

We right-align the pyramid by printing a shrinking number of leading spaces, then printing letters from A up to the current row letter.

Because we print letters using {0,2}, use a monospace font if you want the right edge to look perfect.

⭐ Pattern Output

Output for 5 rows:

Output
    A
   A B
  A B C
 A B C D
A B C D E
1

Complete C# Program (A–E)

First print leading spaces, then print letters A..i using {0,2} formatting.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            for (char i = 'A'; i <= 'E'; i++)
            {
                for (char j = 'E'; j > i; j--)
                    Console.Write(" ");

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

                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Pick the row letter

Outer i runs from A to E.

Rows
2

Print leading spaces

Loop j = E..(i+1) prints one space per step, making the pyramid right-aligned.

Pad
3

Print letters A..i

Loop k = A..i prints each letter in a 2-character field using {0,2}.

Letters
=

Right edge stays fixed

Each row does O(n) work for padding plus letters, so total is O(n²).

2

Variation — User Input Top Letter

Choose the last letter (like E). The pattern prints up to that row.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter top letter (like E): ");
            char top = Convert.ToChar(Console.ReadLine());

            for (char i = 'A'; i <= top; i++)
            {
                for (char j = top; j > i; j--)
                    Console.Write(" ");

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

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Add spaces between letters (e.g. print "{0,2} ")
  • Center the pyramid by using 2-column padding instead of 1 space
  • Print lowercase letters by switching to 'a'
  • Compare with Program 22 (right-aligned sequential stream)

Avoid

  • Using proportional fonts when checking alignment
  • Mixing padding widths (right edge won’t look straight)
  • Letting input exceed Z without validation

Key Takeaways

1

Leading spaces control alignment.

2

Letters restart from A on each row.

3

{0,2} makes columns even.

4

Overall work is O(n²).

❓ Frequently Asked Questions

It gives each letter a fixed-width cell so the pyramid stays aligned even with spaces on the left.
Remove the leading-space loop and print letters directly from A to the row letter.
O(n²) for n rows because each row prints O(n) 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