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 the end, then wrap around 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

C++ Program (Reference Logic)

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

C++
#include <iostream>
using namespace std;

int main() {
    int i, j, k;
    for (i = 65; i <= 69; i++) {
        for (j = i; j <= 69; j++)
            cout << char(j);
        for (k = i; k > 65; k--)
            cout << char(k - 1);
        cout << "\n";
    }
    return 0;
}

🧠 How It Works

1

Outer i is the rotation start

Each row picks a new leading letter from A to E. The line is still a permutation of the same block AE, only rotated so a different letter leads.

Start
2

Forward leg: j from i to E

cout << char(j) prints the suffix from the row start through the top letter (e.g. BCDE when i == 'B').

i..E
3

Wrap leg: k with char(k - 1)

for (k = i; k > 65; k--) prints char(k - 1) each time, emitting i-1, i-2, … down to A without repeating the first letter of the forward segment.

k-1
4

Why every row has five letters

Forward part length 'E' - i + 1 plus wrap part i - 'A' sums to 'E' - 'A' + 1 for this fixed alphabet slice.

5
=

Cyclic shifts

Each row is a rotation of ABCDE. Two inner passes per outer row → O(n²) prints for n letters in the block.

2

Variation — User Input

Generalize the block size with rows (so letters range A.. char('A'+rows-1)).

C++
#include <iostream>
using namespace std;

int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;

    char startChar = 'A';
    char endChar = char('A' + rows - 1);

    for (char i = startChar; i <= endChar; ++i) {
        for (char j = i; j <= endChar; ++j) cout << j;
        for (char k = i; k > startChar; --k) cout << char(k - 1);
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add a space after each letter for readability
  • Validate rows so endChar stays within A.. Z
  • Implement rotation using an array + modulo indexing for larger alphabets

Avoid

  • Printing k (not k - 1) in the wrap loop (duplicates the first letter)
  • Letting rows exceed 26 without handling non-letter ASCII values

Key Takeaways

1

Forward: i..end, then wrap: (i-1)..start.

2

Using k - 1 prevents duplicating the row’s first letter.

3

Every row has the same length: endChar - startChar + 1.

4

O(n²) total output for n letters.

❓ Frequently Asked Questions

Because the first loop already printed i. The wrap should continue with the letter before i, not repeat i again.
When i = A, the wrap loop does not run, so the row is just ABCDE.
O(n²) for n letters because there are n rows each printing n characters.

Explore More C++ Alphabet Patterns!

This is a compact way to generate cyclic shifts without arrays.

All Alphabet Patterns →
Did you know?

If you imagine A B C D E written on a circle, each row is just reading from a different starting point.

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