Diamond-Shaped Alphabet Pattern in C++

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

What You’ll Learn

This pattern is a full diamond made by stacking the inverted V from program 33 and then mirroring it back upward.

The only extra trick: start the bottom half from D so the widest row (E) is printed only once.

⭐ Pattern Output

Output for A.. E:

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

C++ Program (Reference Logic)

Two phases with identical inner loops; second phase starts at D to avoid duplicating the E row.

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

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

🧠 How It Works

1

Top outer loop (grow the V)

i runs 6569 (AE). Each larger i places the letter farther out on both diagonals so the hollow shape opens toward the widest E row.

2

Two inner passes per row

First, j scans E down to A; only when i == j does cout << char(j) run, else a space—this draws the left leg. Then k scans B through E with the same equality rule—this draws the right leg. Together you get the paired letters on each row (a single letter on the first and last rows).

j, k
3

Bottom outer loop (shrink the V)

The second part runs i from 68 down to 65 (DA) with the same inner loops pasted again, rebuilding the arms in reverse so the diamond closes symmetrically.

4

Why the bottom starts at D

The top half already printed the single-wide E row. Starting i at 68 skips repeating that middle line while still mirroring every narrower row.

E
=

Nine rows, two scans each

Five growing rows plus four shrinking rows; every row runs two full passes over the letter ranges. That is O(n²) character decisions for n letters along each leg.

2

Variation — User Input

Enter rows (half height). Bottom half starts at endChar - 1 to avoid duplicating the center row.

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

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

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

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

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Fill the interior between the legs for a solid diamond
  • Use two spaces per blank to stretch the diamond
  • Keep rows within 26 for A..Z

Avoid

  • Starting the bottom half at endChar (duplicates the center row)
  • Proportional fonts when you want perfect alignment

Key Takeaways

1

Diamond = top half + mirrored bottom half.

2

Start bottom half at endChar - 1 to avoid a duplicate center.

3

Rows and width both scale as \(2n-1\).

4

O(n²) time for n letters.

❓ Frequently Asked Questions

Because the second phase begins at endChar - 1, skipping the already-printed endChar row.
O(n²) for n letters.

Explore More Pattern Programs!

Once you can mirror a shape vertically, you can create diamonds, hourglasses, and many other symmetric patterns.

C++ Number Patterns →
Did you know?

The same row-printing logic is reused for both halves—only the range of the outer loop changes to mirror the output.

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