Decreasing & Increasing Alphabet Rows in C#

What You’ll Learn
Each row is built from two parts: a short descending prefix from the row letter down to B, followed by an ascending suffix from A up to a calculated cap so every row stays the same width.
For A…E, the cap is A + E - i (in indices: 0..(4 - i)).
⭐ Pattern Output
Output for A…E (no spaces):
ABCDE
BABCD
CBABC
DCBAB
EDCBAComplete C# Program (A–E)
Matches the reference logic: print alpha[i]…alpha[1], then alpha[0]…alpha[4 - i].
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 = i; j > 0; j--)
Console.Write(alpha[j]);
for (int k = 0; k <= 4 - i; k++)
Console.Write(alpha[k]);
Console.WriteLine();
}
}
}
}🧠 How It Works
Row index sets the starting letter
When i = 0 start at A; when i = 4 start at E.
Descending prefix (skip A)
Loop j from i down to 1. This prints the row letter down to B and avoids duplicating A at the join.
Ascending suffix fills remaining width
Print A through A + E - i (indices 0..(4 - i)) so the total row length is always 5 for A..E.
Same width every row
Left side shrinks while the right side grows, keeping the total row length constant.
Variation — Choose the End Letter
This version prints the same idea for A…end. Cap is computed as A + end - i.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter end letter (like E): ");
char end = Convert.ToChar(Console.ReadLine());
int n = end - 'A';
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 0; i <= n; i++)
{
for (int j = i; j > 0; j--)
Console.Write(alpha[j]);
for (int k = 0; k <= n - i; k++)
Console.Write(alpha[k]);
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Print a space between letters for readability
- Change the start letter from
Ato another character - Print each row into a
StringBuilderbefore output - Compare with Program 24 for a palindrome split around
A
Avoid
- Letting
endgo pastZwithout validation - Printing
Ain both parts (would duplicate at the join) - Changing loop bounds without re-checking row width
Key Takeaways
Each row is a decreasing prefix + increasing suffix.
Skip A in the first part to avoid duplication.
Cap A + E - i keeps constant row width.
Overall work is O(n²).
❓ Frequently Asked Questions
A is printed by the second (ascending) loop. If the first loop also printed A, the join would duplicate it.A..E, the cap is A + E - i (indices: 0..(4-i)). This keeps each row the same width.12 people found this page helpful
