Reverse Centered Alphabet Pyramid in C++

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

What You’ll Learn

This reverse centered alphabet pyramid reuses the row logic from program 28: print downward from E to A, then upward from B to E so the center row is not repeated.

Each cell still uses the same rule: if j > i, print j, else print i.

⭐ Pattern Output

Nine rows for AE (space after each letter):

Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
E D C B B B C D E
E D C C C C C D E
E D D D D D D D E
E E E E E E E E E
1

C++ Program (Reference Logic)

Two phases share the same two inner loops; the second phase starts at B to skip the duplicate center.

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

int main() {
    int i, j;
    int k = 69;

    // Upper half (E -> A)
    for (i = k; i >= 65; i--) {
        for (j = k; j >= 65; j--) {
            if (j > i) cout << char(j) << " ";
            else cout << char(i) << " ";
        }
        for (j = 66; j <= k; j++) {
            if (j > i) cout << char(j) << " ";
            else cout << char(i) << " ";
        }
        cout << "\n";
    }

    // Lower half (B -> E)
    for (i = 66; i <= k; i++) {
        for (j = k; j >= 65; j--) {
            if (j > i) cout << char(j) << " ";
            else cout << char(i) << " ";
        }
        for (j = 66; j <= k; j++) {
            if (j > i) cout << char(j) << " ";
            else cout << char(i) << " ";
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Upper half: i from E down to A

Each row is a full-width “floor” line: on both inner sweeps, if j > i then cout << char(j) << ' ', else cout << char(i) << ' '. That is the same cell rule as program 28, repeated for each floor from E down to A.

Down
2

Center row at i == 'A'

The middle text line is E D C B A B C D E. Here the floor is A, so the left and right segments meet with a single A between descending and ascending halves.

A
3

Lower half: i from B back to E

A second outer loop increases i again with the same inner structure, rebuilding the upper rows in reverse so the pyramid completes without duplicating the widest border row.

Up
4

Nine symbol rows for five letters

Each line prints nine letter tokens plus separating spaces from cout << ' '. You get 2 × 5 - 1 = 9 rows: five steps down to the A floor, then four steps back up to the outer E frame.

9
=

Symmetry

Adjacent rows around the center differ only in the middle run (A vs B here). Nested sweeps per row keep the complexity at O(n²) for span n.

2

Variation — User Input

Pick rows (alphabet span). The peak letter is endChar = 'A' + rows - 1.

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

static void printRow(char i, char startChar, char endChar) {
    for (char j = endChar; j >= startChar; --j) {
        cout << (j > i ? j : i) << ' ';
    }
    for (char j = char(startChar + 1); j <= endChar; ++j) {
        cout << (j > i ? j : i) << ' ';
    }
    cout << "\n";
}

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

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

    for (char i = endChar; i >= startChar; --i) {
        printRow(i, startChar, endChar);
    }
    for (char i = char(startChar + 1); i <= endChar; ++i) {
        printRow(i, startChar, endChar);
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Extract the repeated row-printing logic into a function (as in the variation)
  • Remove the trailing space for a compact output block
  • Keep rows <= 26 to stay in A..Z

Avoid

  • Starting the second phase at A (duplicates the center row)
  • Changing only one half’s loop bounds (breaks symmetry)

Key Takeaways

1

Two phases (down then up) move the floor letter from the outer edge to A and back, keeping the same row rule.

2

Lower half starts at B to avoid duplicating the center.

3

Total rows for n letters is 2n - 1.

4

O(n²) output for span n.

❓ Frequently Asked Questions

Because the direction of i changes at the center. Splitting makes it easy to skip the duplicate center row.
You print (2n - 1) rows and (2n - 1) cells per row, so total is (2n - 1)². For n = 5, that is 9² = 81 cells (plus spaces/newlines).
O(n²) for span n.

Explore More C++ Alphabet Patterns!

Try building a hollow variant by printing the shell letters only when you are on the border.

All Alphabet Patterns →
Did you know?

For n letters you print \((2n - 1)\) rows and \((2n - 1)\) letter cells per row, so \((2n - 1)^2\) cells in total (plus spaces and newlines).

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