V-Shaped Alphabet Pattern in C#

What You’ll Learn
This pattern prints letters only on the two diagonals that form a V. Everywhere else, it prints spaces.
The last row prints a single vertex letter (E), because the right diagonal intentionally skips the last letter.
⭐ Pattern Output
Output for 5 letters (width \(2n-1 = 9\)):
A A
B B
C C
D D
EComplete C# Program (A–E)
Two scans per row: left-to-right (A..E) and right-to-left (D..A). Letters print only when the row matches the column.
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 <= 4; j++)
{
if (i == j) Console.Write(alpha[j]);
else Console.Write(" ");
}
for (int k = 3; k >= 0; k--)
{
if (i == k) Console.Write(alpha[k]);
else Console.Write(" ");
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Outer loop chooses the row letter
Row index i runs from 0..4 which maps to A.. E.
Left leg (main diagonal)
Loop j = 0..4. Print the letter only when i == j; otherwise print a space.
Right leg (skip E to keep a single vertex)
Loop k = 3..0 (D..A). Printing starts one letter below E so the last row has only one E at the vertex.
Two diagonals form a V
Each row prints a single letter on the left diagonal, plus one on the right diagonal (except the last row).
Variation — Choose the Top Letter
Let the user pick the end letter (like E). The right scan starts at end - 1 so the vertex prints once.
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 = 0; j <= n; j++)
Console.Write(i == j ? alpha[j] : ' ');
for (int k = n - 1; k >= 0; k--)
Console.Write(i == k ? alpha[k] : ' ');
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Use a dot (
.) instead of spaces to visualize alignment while debugging - Increase the letter range (up to Z) for a larger V
- Print with a trailing space to make layout clearer in proportional fonts
- Compare with Program 20 for diagonal alignment ideas
Avoid
- Starting the right loop at
end(it duplicates the vertex) - Using a proportional font when checking alignment
- Accepting input beyond
Zwithout validation
Key Takeaways
Only diagonal positions print letters; the rest are spaces.
Use two scans to form the left and right legs.
Skip the last letter in the right leg to keep one vertex.
Total work is O(n²).
❓ Frequently Asked Questions
D (end-1), so it never matches the last letter E. That keeps a single vertex at the bottom.2n-1: n columns in the left block and n-1 columns in the right block.12 people found this page helpful
