Rotating Alphabet Pattern in C#

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:
ABCDE
BCDEA
CDEBA
DECBA
EDCBAComplete C# Program (A–E)
Forward run i..E plus wrap run using k - 1.
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
Row start index
i is the row start (0..4), which corresponds to letters A..E.
Forward part
Print alpha[i] through alpha[4].
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.
Same length each row
Each row prints 5 letters, so total output is O(n²) for n letters.
Variation — User Input Top Letter
Choose how many letters to rotate (A..top). This version uses character loops directly.
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
kinstead ofk-1in 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
Print forward from the row start to the end.
Wrap by printing earlier letters in reverse.
Use k-1 to avoid duplicating the boundary letter.
Each row has constant length, so work is O(n²).
❓ Frequently Asked Questions
12 people found this page helpful
