Palindromic Alphabet Pyramid (BAB) in C#

What You’ll Learn
Build each row as a palindrome by stitching a descending run (i down to B) with an ascending run (A up to i).
This version prints letters with no extra spacing (unlike right-aligned patterns that use 2-column cells).
⭐ Pattern Output
Output for 5 rows:
A
BAB
CBABC
DCBABCD
EDCBABCDEComplete C# Program (A–E)
Two loops per row: descending (i..B), then ascending (A..i). The descending loop stops before A, so A appears once.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
for (char i = 'A'; i <= 'E'; i++)
{
for (char j = i; j > 'A'; j--)
Console.Write(j);
for (char j = 'A'; j <= i; j++)
Console.Write(j);
Console.WriteLine();
}
}
}
}🧠 How It Works
Outer loop picks the row peak
i goes from A to E.
Print the left wing (descending)
Loop j = i..B prints the descending part. We stop at j > 'A' so A is not printed here.
Print the center + right wing (ascending)
Loop j = A..i prints the center A and then climbs back up to the peak letter.
One A in the middle
Row lengths are 1, 3, 5, 7, 9 for A..E, so total characters is n² for n rows.
Variation — User Input Rows
Let the user choose how many rows to print (up to 26 for A..Z).
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter number of rows (1..26): ");
int n = int.Parse(Console.ReadLine());
if (n < 1) return;
if (n > 26) n = 26;
for (int r = 0; r < n; r++)
{
char peak = (char)('A' + r);
for (char ch = peak; ch > 'A'; ch--)
Console.Write(ch);
for (char ch = 'A'; ch <= peak; ch++)
Console.Write(ch);
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Add spaces between letters to make the pyramid wider
- Center the rows using leading spaces (see Program 16)
- Switch to lowercase by using
'a'as the base - Combine with star separators to create hybrid patterns
Avoid
- Using
j >= 'A'in the descending loop (it duplicatesA) - Letting row count exceed 26 without deciding how to handle letters after Z
- Assuming the output is centered (this pattern is left-aligned by default)
Key Takeaways
Two loops per row: descending then ascending.
Stop the descending loop at > 'A' to avoid duplicating A.
Row length is 2r-1 (odd numbers).
Total printed characters over n rows is n².
❓ Frequently Asked Questions
j > 'A', and the ascending loop starts at 'A'. That prevents printing A twice at the join.'E' to your target letter (like 'H').12 people found this page helpful
