Symmetric Alphabet Pyramid in C#

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

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):

Output
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
1

Complete C# Program (A–E)

Matches the reference logic using a bridge variable n = k - 1 to print the descending half.

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

1

Add leading spaces

Loop j from E down to the current row to center the pyramid.

Padding
2

Print ascending half

Loop k = A..i and print alpha[k].

Up
3

Mirror without duplicating the center

Set n = k - 1, then print --n repeatedly so the tail is (i-1)..A.

Mirror
=

Centered palindromes

Each row is an alphabet palindrome: it climbs from A to the row letter, then mirrors back to A.

2

Variation — Choose the Top Letter

Let the user choose the top letter (like E). The loops adapt to the new size automatically.

C#
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 StringBuilder before 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 Z without validation

Key Takeaways

1

Leading spaces center the pyramid.

2

Ascending half prints A..i.

3

Descending half prints (i-1)..A without repeating the middle.

4

Total work is O(n²).

❓ Frequently Asked Questions

After printing A..i, the mirror part starts from i-1 down to A. That avoids duplicating the peak letter.
After the ascending loop, 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.
O(n²) for n rows because each row prints O(n) spaces/letters combined.

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