Palindromic Alphabet Pyramid in C#

What You’ll Learn
Each row is a palindrome: go up from A to the row’s peak letter, then come back down without repeating the peak.
Compare Program 17 (diagonal *) and Program 1 (only the left half triangle).
⭐ Pattern Output
Five rows, no spaces between letters:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBAComplete C# Program (A–E)
This version uses the same approach as the reference: print the forward part A..i, then print back from i-1..A.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= i; j++)
Console.Write(alpha[j]);
for (int k = i - 1; k >= 0; k--)
Console.Write(alpha[k]);
Console.WriteLine();
}
}
}
}🧠 How It Works
Pick the row peak
Row i ends at alpha[i].
Print the forward half
Loop j = 0..i prints A through the peak letter.
Mirror back down
Loop k = i-1..0 prints the reverse half without duplicating the peak.
A palindrome each row
Row lengths are 1, 3, 5, ... so the total printed characters over n rows is n².
Variation — User Input Size
Choose how many rows to print. This version uses characters directly (no array) and computes the peak letter for each row.
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());
for (int r = 0; r < n; r++)
{
char peak = (char)('A' + r);
for (char ch = 'A'; ch <= peak; ch++)
Console.Write(ch);
for (char ch = (char)(peak - 1); ch >= 'A'; ch--)
Console.Write(ch);
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Add spaces between letters to make the pyramid wider
- Center the rows with leading spaces (see Program 16)
- Print lowercase letters by using
'a'as the base - Limit input so it doesn’t exceed
Z
Avoid
- Starting the mirror from the peak letter (it duplicates the center)
- Mixing 0-based and 1-based row counts without adjusting the peak
- Letting large input overflow the alphabet range
Key Takeaways
Use two loops per row: forward, then reverse.
Start the reverse half at peak - 1 to avoid duplication.
Row length is 2r - 1.
Total work over n rows is O(n²).
❓ Frequently Asked Questions
A..peak, then print (peak-1)..A so the center letter is not duplicated.alpha[x] + " " instead of alpha[x] in both halves, and update the sample output accordingly.12 people found this page helpful
