Symmetric Alphabet Pyramid in C++

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

What You’ll Learn

This pattern prints centered palindromic rows: A, ABA, ABCBA, up to ABCDEDCBA.

We pad with spaces, print the left half ascending, then print the right half descending without repeating the middle letter.

⭐ Pattern Output

Output for 5 rows (A..E):

Output
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
1

C++ Program (Reference Logic)

Uses an n = k - 1 bridge variable like the reference to print the mirrored tail.

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

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

🧠 How It Works

1

Leading spaces

j runs from 69 down to i; each step prints " " with cout so shorter rows shift right and the pyramid aligns under the widest line.

Space
2

Ascending half

k from 65 through i prints char(k), building the left side including the peak letter for that row.

Up
3

n = k - 1

When the second loop exits, k == i + 1. Setting n = k - 1 puts n back on the peak so the first --n yields i - 1 for the mirror.

Bridge
4

Mirror with --n

m runs i - 65 times (zero when i == 65). Each cout << char(--n) prints the next letter down without repeating the center character.

Down
=

Palindrome row

The peak prints only in the ascending loop; the descending loop walks from i - 1 down to A. Five outer rows with proportional inner work → O(n²) for n letters.

2

Variation — User Input (char literals)

Same pattern, but written with character variables ('A'.. endChar).

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 = endChar; j >= i; --j) cout << ' ';

        for (char k = startChar; k <= i; ++k) cout << k;

        for (char k = char(i - 1); k >= startChar; --k) cout << k;

        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Print a space between letters for a wider pyramid
  • Use lowercase by starting at 'a'
  • Limit rows so output stays within letters A..Z

Avoid

  • Printing the peak letter again on the way down (duplicates the center)
  • Mixing different padding widths without checking alignment

Key Takeaways

1

Build rows as palindromes: ascending then descending.

2

Don’t duplicate the middle letter: start descending from i - 1.

3

Leading spaces keep the pyramid centered.

4

O(n²) output for n rows.

❓ Frequently Asked Questions

Because the descending half starts at i - 1 (or uses --n), so the peak is printed only once.
O(n²) for n rows.

Explore More C++ Alphabet Patterns!

Once you can build one palindromic row, you can generate diamonds and more complex symmetric patterns.

All Alphabet Patterns →
Did you know?

Row lengths here are odd: 1, 3, 5, 7, 9… because each row is a palindrome with one center character.

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