Diamond-Shaped Alphabet Pattern in C#

What You’ll Learn
This pattern is a full diamond made by stacking the inverted V from program 33 and then mirroring it back upward.
The only extra trick: start the bottom half from D so the widest row (E) is printed only once.
⭐ Pattern Output
Output for A…E (width \(2n-1 = 9\)):
A
B B
C C
D D
E E
D D
C C
B B
AComplete C# Program (A–E)
Two phases with identical inner loops; the second phase starts at D to avoid duplicating the E row.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
// Top half: A through E
for (int i = 0; i <= 4; i++)
{
for (int j = 4; j >= 0; j--)
{
if (i == j) Console.Write(alpha[j]);
else Console.Write(" ");
}
for (int k = 1; k <= 4; k++)
{
if (i == k) Console.Write(alpha[k]);
else Console.Write(" ");
}
Console.WriteLine();
}
// Bottom half: D through A (avoid repeating E row)
for (int i = 3; i >= 0; i--)
{
for (int j = 4; j >= 0; j--)
{
if (i == j) Console.Write(alpha[j]);
else Console.Write(" ");
}
for (int k = 1; k <= 4; k++)
{
if (i == k) Console.Write(alpha[k]);
else Console.Write(" ");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Reuse the inverted-V row logic
Each row prints a left diagonal via a reverse scan (E..A) and a right diagonal via a forward scan (B..E).
Top half grows A..E
Outer loop prints rows for A through E.
Bottom half mirrors D..A
Start at D so the widest E row is not duplicated.
Top + mirror = diamond
Build the top half from A to E, then mirror back down from D to A.
Variation — Choose the Top Letter
Let the user choose the end letter (like E). Build the top half (A..end) then mirror back (end-1..A).
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter top 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 = n; j >= 0; j--)
Console.Write(i == j ? alpha[j] : ' ');
for (int k = 1; k <= n; k++)
Console.Write(i == k ? alpha[k] : ' ');
Console.WriteLine();
}
for (int i = n - 1; i >= 0; i--)
{
for (int j = n; j >= 0; j--)
Console.Write(i == j ? alpha[j] : ' ');
for (int k = 1; k <= n; k++)
Console.Write(i == k ? alpha[k] : ' ');
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Use dots instead of spaces to debug alignment
- Print a trailing space to make width visible in proportional viewers
- Combine both halves into one loop using an index mapping
- Compare with Program 33 (top half only)
Avoid
- Starting the bottom half at
end(duplicates the widest row) - Using a proportional font when checking alignment
- Allowing input beyond
Zwithout validation
Key Takeaways
Diamond = top half + mirrored bottom half.
Bottom half starts at D to avoid duplicate center.
Width and rows are both 2n-1.
Total work is O(n²).
❓ Frequently Asked Questions
E) is already printed at the end of the top half. Starting from D avoids duplicating that middle row.A..E and then mirrored back for D..A using the same inner loops.12 people found this page helpful
