Symmetric Alphabet Pyramid in C#

What You’ll Learn
This pattern prints centered palindromic rows: A, ABA, ABCBA, up to ABCDEDCBA.
We pad with spaces, print the left half ascending, then print the right half descending without repeating the middle letter.
⭐ Pattern Output
Output for 5 rows (A..E):
A
ABA
ABCBA
ABCDCBA
ABCDEDCBAComplete C# Program (A–E)
Matches the reference logic using a bridge variable n = k - 1 to print the descending half.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j, k, m, n;
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (i = 0; i <= 4; i++)
{
for (j = 4; j > i; j--)
Console.Write(" ");
for (k = 0; k <= i; k++)
Console.Write(alpha[k]);
n = k - 1;
for (m = 0; m < i; m++)
Console.Write(alpha[--n]);
Console.WriteLine();
}
}
}
}🧠 How It Works
Add leading spaces
Loop j from E down to the current row to center the pyramid.
Print ascending half
Loop k = A..i and print alpha[k].
Mirror without duplicating the center
Set n = k - 1, then print --n repeatedly so the tail is (i-1)..A.
Centered palindromes
Each row is an alphabet palindrome: it climbs from A to the row letter, then mirrors back to A.
Variation — Choose the Top Letter
Let the user choose the top letter (like E). The loops adapt to the new size automatically.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter top letter (like E): ");
char top = Convert.ToChar(Console.ReadLine());
int end = top - 'A';
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 0; i <= end; i++)
{
for (int j = end; j >= i; j--)
Console.Write(" ");
for (int k = 0; k <= i; k++)
Console.Write(alpha[k]);
int n = k - 1;
for (int m = 0; m < i; m++)
Console.Write(alpha[--n]);
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Print a space between letters for readability
- Use lowercase letters by switching the alphabet string
- Build each row with a
StringBuilderbefore printing - Compare with Program 18 for another palindrome approach
Avoid
- Duplicating the center letter when mirroring
- Using proportional fonts when checking alignment
- Accepting input beyond
Zwithout validation
Key Takeaways
Leading spaces center the pyramid.
Ascending half prints A..i.
Descending half prints (i-1)..A without repeating the middle.
Total work is O(n²).
❓ Frequently Asked Questions
A..i, the mirror part starts from i-1 down to A. That avoids duplicating the peak letter.k is one step past the peak. Setting n = k - 1 restores the peak, and then --n begins the descending side from the previous letter.12 people found this page helpful
