Rotating Alphabet Pattern in C#

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

What You’ll Learn

Each row is a cyclic rotation of the same set of letters. We print from the row start to E, then wrap by printing the earlier letters.

This output uses adjacent letters (no spaces), matching the reference.

⭐ Pattern Output

Output for 5 rows:

Output
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
1

Complete C# Program (A–E)

Forward run i..E plus wrap run using k - 1.

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 <= 4; j++)
                    Console.Write(alpha[j]);

                for (int k = i; k > 0; k--)
                    Console.Write(alpha[k - 1]);

                Console.WriteLine();
            }
        }
    }
}

🧠 How It Works

1

Row start index

i is the row start (0..4), which corresponds to letters A..E.

Start
2

Forward part

Print alpha[i] through alpha[4].

Forward
3

Wrap part without duplication

Count down from k = i and print alpha[k-1]. This appends earlier letters (like A) but doesn’t repeat the row start.

Wrap
=

Same length each row

Each row prints 5 letters, so total output is O(n²) for n letters.

2

Variation — User Input Top Letter

Choose how many letters to rotate (A..top). This version uses character loops directly.

C#
using System;

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

            for (char i = 'A'; i <= top; i++)
            {
                for (char j = i; j <= top; j++)
                    Console.Write(j);

                for (char k = i; k > 'A'; k--)
                    Console.Write((char)(k - 1));

                Console.WriteLine();
            }
        }
    }
}

💡 Tips for Enhancement

Try These

  • Print spaces between letters to visualize the wrap more clearly
  • Generalize to lowercase by using 'a'..
  • Rotate a custom string instead of A..E
  • Compare with Program 25 (shrinking rows with k++)

Avoid

  • Printing k instead of k-1 in the wrap loop (duplicates the boundary letter)
  • Letting the user enter non-letters without validation
  • Changing the ranges without updating row length assumptions

Key Takeaways

1

Print forward from the row start to the end.

2

Wrap by printing earlier letters in reverse.

3

Use k-1 to avoid duplicating the boundary letter.

4

Each row has constant length, so work is O(n²).

❓ Frequently Asked Questions

Each row prints from the row start to the end, then prints earlier letters to complete the row. That creates the rotation effect.
It avoids duplicating the boundary letter when you wrap (otherwise the join repeats one character).
O(n²) for n rows because 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