V-Shaped Alphabet Pattern in C#

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

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\)):

Output
A       A
 B     B
  C   C
   D D
    E
1

Complete 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.

C#
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

1

Outer loop chooses the row letter

Row index i runs from 0..4 which maps to A.. E.

Rows
2

Left leg (main diagonal)

Loop j = 0..4. Print the letter only when i == j; otherwise print a space.

Left
3

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.

Right
=

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).

2

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.

C#
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 Z without validation

Key Takeaways

1

Only diagonal positions print letters; the rest are spaces.

2

Use two scans to form the left and right legs.

3

Skip the last letter in the right leg to keep one vertex.

4

Total work is O(n²).

❓ Frequently Asked Questions

The right scan starts at D (end-1), so it never matches the last letter E. That keeps a single vertex at the bottom.
For n letters, width is 2n-1: n columns in the left block and n-1 columns in the right block.
O(n²) for n letters because each of n rows scans O(n) positions.

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