Palindromic Alphabet Pyramid (BAB) in C#

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

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:

Output
A
BAB
CBABC
DCBABCD
EDCBABCDE
1

Complete 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.

C#
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

1

Outer loop picks the row peak

i goes from A to E.

Peak
2

Print the left wing (descending)

Loop j = i..B prints the descending part. We stop at j > 'A' so A is not printed here.

Down
3

Print the center + right wing (ascending)

Loop j = A..i prints the center A and then climbs back up to the peak letter.

Up
=

One A in the middle

Row lengths are 1, 3, 5, 7, 9 for A..E, so total characters is for n rows.

2

Variation — User Input Rows

Let the user choose how many rows to print (up to 26 for A..Z).

C#
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 duplicates A)
  • 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

1

Two loops per row: descending then ascending.

2

Stop the descending loop at > 'A' to avoid duplicating A.

3

Row length is 2r-1 (odd numbers).

4

Total printed characters over n rows is .

❓ Frequently Asked Questions

The descending loop stops at j > 'A', and the ascending loop starts at 'A'. That prevents printing A twice at the join.
Change the outer loop upper bound from 'E' to your target letter (like 'H').
O(n²) for n rows, since each row prints O(n) characters.

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