Reverse Centered Alphabet Pyramid in C#

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 A…E (space after each letter):
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 EComplete C# Program (A–E)
Same row logic as program 28, printed in two phases to complete the reverse centered pyramid.
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
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.
Upper half: i goes down
Run i = E..A to reach the center row with A.
Lower half: i goes up (skip center)
Start the second loop at B (i = 1) to avoid printing the center line twice.
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.
Upper + lower = closed pyramid
Print down to the center (A row), then the matching bands back up, without repeating the center row.
Variation — User Input Top Letter
Pick the top letter (like E) and print the reverse centered pyramid for A..top.
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
Zwithout validation - Mixing different alphabets/arrays without adjusting
k
Key Takeaways
Two phases: upper (E..A) then lower (B..E) with the same row rule for the full pyramid.
Condition j > i decides border vs interior.
Total rows for n letters are 2n-1.
Total work is O(n²).
❓ Frequently Asked Questions
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.A-center row is already printed in the upper half. Starting at B avoids duplicating the center row.12 people found this page helpful
