Decreasing & Increasing Alphabet Rows in C#

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

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 AE, the cap is A + E - i (in indices: 0..(4 - i)).

⭐ Pattern Output

Output for AE (no spaces):

Output
ABCDE
BABCD
CBABC
DCBAB
EDCBA
1

Complete C# Program (A–E)

Matches the reference logic: print alpha[i]alpha[1], then alpha[0]alpha[4 - i].

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 = 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

1

Row index sets the starting letter

When i = 0 start at A; when i = 4 start at E.

Rows
2

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.

Left
3

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.

Right
=

Same width every row

Left side shrinks while the right side grows, keeping the total row length constant.

2

Variation — Choose the End Letter

This version prints the same idea for Aend. Cap is computed as A + end - i.

C#
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 A to another character
  • Print each row into a StringBuilder before output
  • Compare with Program 24 for a palindrome split around A

Avoid

  • Letting end go past Z without validation
  • Printing A in both parts (would duplicate at the join)
  • Changing loop bounds without re-checking row width

Key Takeaways

1

Each row is a decreasing prefix + increasing suffix.

2

Skip A in the first part to avoid duplication.

3

Cap A + E - i keeps constant row width.

4

Overall work is O(n²).

❓ Frequently Asked Questions

Because A is printed by the second (ascending) loop. If the first loop also printed A, the join would duplicate it.
For A..E, the cap is A + E - i (indices: 0..(4-i)). This keeps each row the same width.
O(n²) for n letters because you print n rows and each row prints O(n) letters.

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