Palindromic Alphabet Pyramid in C#

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

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:

Output
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
1

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

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

1

Pick the row peak

Row i ends at alpha[i].

Peak
2

Print the forward half

Loop j = 0..i prints A through the peak letter.

Up
3

Mirror back down

Loop k = i-1..0 prints the reverse half without duplicating the peak.

Down
=

A palindrome each row

Row lengths are 1, 3, 5, ... so the total printed characters over n rows is .

2

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.

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

1

Use two loops per row: forward, then reverse.

2

Start the reverse half at peak - 1 to avoid duplication.

3

Row length is 2r - 1.

4

Total work over n rows is O(n²).

❓ Frequently Asked Questions

Print A..peak, then print (peak-1)..A so the center letter is not duplicated.
Yes. Print alpha[x] + " " instead of alpha[x] in both halves, and update the sample output accordingly.
O(n²) for n rows because 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