Reverse Centered Alphabet Pyramid in C#

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

What You’ll Learn

This reverse centered alphabet pyramid reuses the row logic from program 28: print downward from E to A, then upward from B to E so the center row is not repeated.

Each cell uses the same rule: if j > i, print the column letter; otherwise print the current row floor letter.

⭐ Pattern Output

Nine rows for AE (space after each letter):

Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E
1

Complete C# Program (A–E)

Same row logic as program 28, printed in two phases to complete the reverse centered pyramid.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            int k = 4; // index for 'E'
            char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

            // Upper half (E down to A)
            for (int i = k; i >= 0; i--)
            {
                for (int j = k; j >= 0; j--)
                {
                    Console.Write(j > i ? $"{alpha[j]} " : $"{alpha[i]} ");
                }
                for (int j = 1; j <= k; j++)
                {
                    Console.Write(j > i ? $"{alpha[j]} " : $"{alpha[i]} ");
                }
                Console.WriteLine();
            }

            // Lower half (B up to E) to avoid repeating the A row
            for (int i = 1; i <= k; i++)
            {
                for (int j = k; j >= 0; j--)
                {
                    Console.Write(j > i ? $"{alpha[j]} " : $"{alpha[i]} ");
                }
                for (int j = 1; j <= k; j++)
                {
                    Console.Write(j > i ? $"{alpha[j]} " : $"{alpha[i]} ");
                }
                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Reuse the row rule

Each row prints a left scan (E..A) and a right scan (B..E). Each cell prints j when j > i, otherwise prints i.

Logic
2

Upper half: i goes down

Run i = E..A to reach the center row with A.

Upper
3

Lower half: i goes up (skip center)

Start the second loop at B (i = 1) to avoid printing the center line twice.

Lower
4

Keep the row width constant

Each row prints the same number of cells: left half has k+1 letters and right half has k letters, so total width is 2k+1. That keeps every row aligned in the output grid.

Width
=

Upper + lower = closed pyramid

Print down to the center (A row), then the matching bands back up, without repeating the center row.

2

Variation — User Input Top Letter

Pick the top letter (like E) and print the reverse centered pyramid for A..top.

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 k = top - 'A';
            char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

            for (int i = k; i >= 0; i--)
            {
                for (int j = k; j >= 0; j--)
                    Console.Write(j > i ? $"{alpha[j]} " : $"{alpha[i]} ");
                for (int j = 1; j <= k; j++)
                    Console.Write(j > i ? $"{alpha[j]} " : $"{alpha[i]} ");
                Console.WriteLine();
            }

            for (int i = 1; i <= k; i++)
            {
                for (int j = k; j >= 0; j--)
                    Console.Write(j > i ? $"{alpha[j]} " : $"{alpha[i]} ");
                for (int j = 1; j <= k; j++)
                    Console.Write(j > i ? $"{alpha[j]} " : $"{alpha[i]} ");
                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Change the separator (space) to two spaces for a wider look
  • Build a string per row to avoid trailing spaces
  • Use the same approach with numbers (5..1) to create a numeric pattern with a similar two-phase layout
  • Compare with Program 21 for other layered pattern ideas

Avoid

  • Starting the lower half at A (it duplicates the center row)
  • Accepting input beyond Z without validation
  • Mixing different alphabets/arrays without adjusting k

Key Takeaways

1

Two phases: upper (E..A) then lower (B..E) with the same row rule for the full pyramid.

2

Condition j > i decides border vs interior.

3

Total rows for n letters are 2n-1.

4

Total work is O(n²).

❓ Frequently Asked Questions

The floor letter moves from the peak down to A at the center, then back up to the peak, while each row stays full width (2n-1 cells for n letters). Program 28’s row logic runs down to A, then again from B up without repeating the center line.
The A-center row is already printed in the upper half. Starting at B avoids duplicating the center row.
O(n²) for n letters because there are O(n) rows and each row prints O(n) cells.

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