Mirrored Alphabet, Spaces in C#

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

What You’ll Learn

Build rows that widen toward the middle: letters on the left, a shrinking space band, then the mirrored letters on the right.

Compare Program 18 (palindrome without a gap) and Program 15 (stars in the middle).

⭐ Pattern Output

Five rows from A to E (spaces shown as gaps in monospace):

Output
A        A
AB      BA
ABC    CBA
ABCD  DCBA
ABCDEEDCBA
1

Complete C# Program (A–E)

Two fixed-width scans per row. Conditions decide whether to print a letter or a space.

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 (j <= i)
                        Console.Write(alpha[j]);
                    else
                        Console.Write(" ");
                }

                for (int k = 4; k >= 0; k--)
                {
                    if (k > i)
                        Console.Write(" ");
                    else
                        Console.Write(alpha[k]);
                }

                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Fix the width

Both halves scan a fixed range (0..4), so each row has a consistent total width.

Width
2

Paint the left ramp

For column j, print alpha[j] if j <= i, else print a space.

Left
3

Paint the right ramp

Scan from the end: while k > i print spaces; once k <= i, print alpha[k] to mirror the left.

Right
=

Gap shrinks each row

On the last row (E), both halves print only letters, meeting as ABCDEEDCBA.

2

Variation — User Input (Top Letter)

Let the user choose the last letter (like E). The code builds the full width dynamically.

C#
using System;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the top letter (like E): ");
            char top = Convert.ToChar(Console.ReadLine());

            int n = top - 'A';
            char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();

            for (int i = 0; i <= n; i++)
            {
                for (int j = 0; j <= n; j++)
                    Console.Write(j <= i ? alpha[j] : ' ');

                for (int k = n; k >= 0; k--)
                    Console.Write(k > i ? ' ' : alpha[k]);

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Replace the middle spaces with a symbol (see Program 15)
  • Use lowercase by switching to 'a' as the base
  • Center the whole line by adding leading spaces
  • Make the gap more visible by printing . instead of spaces while testing

Avoid

  • Using different widths for left and right halves (mirroring breaks)
  • Printing tabs instead of spaces (alignment changes by editor)
  • Letting top exceed Z without handling it

Key Takeaways

1

Two fixed-width passes build left and right halves.

2

j <= i prints letters on the left.

3

k > i prints spaces on the right until the mirror starts.

4

The gap shrinks to zero on the last row.

❓ Frequently Asked Questions

The spaces keep both halves fixed width so the mirror effect is aligned. The gap shrinks each row until both halves touch.
Increase the last index/letter and update the loop bounds so the left and right halves each scan the new width.
O(n²) for n letters because each row prints O(n) cells across two halves.

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