Right-Aligned Alphabet Pyramid in C#

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:
A
A B
A B C
A B C D
A B C D EComplete C# Program (A–E)
First print leading spaces, then print letters A..i using {0,2} formatting.
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
Pick the row letter
Outer i runs from A to E.
Print leading spaces
Loop j = E..(i+1) prints one space per step, making the pyramid right-aligned.
Print letters A..i
Loop k = A..i prints each letter in a 2-character field using {0,2}.
Right edge stays fixed
Each row does O(n) work for padding plus letters, so total is O(n²).
Variation — User Input Top Letter
Choose the last letter (like E). The pattern prints up to that row.
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
Zwithout validation
Key Takeaways
Leading spaces control alignment.
Letters restart from A on each row.
{0,2} makes columns even.
Overall work is O(n²).
❓ Frequently Asked Questions
A to the row letter.12 people found this page helpful
