Diamond Alphabet & Stars in C++

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

What You’ll Learn

Build a vertical diamond where each horizontal line is the same letter separated by *, widening to the middle then narrowing again.

Compare C++ star diamonds (geometry only) and program 15 (mirror with a star block).

⭐ Pattern Output

Half height 5:

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

Complete C++ Program (Half Height 5)

Odd j prints the row letter, even j prints *. Upper half prints 1..5, lower half prints 4..1.

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

int main() {
    int i, j;
    for (i = 1; i <= 5; i++) {
        for (j = 1; j < i * 2; j++) {
            if (j % 2 == 0) cout << "*";
            else cout << char(i + 64);
        }
        cout << "\n";
    }
    for (i = 4; i >= 1; i--) {
        for (j = 1; j < i * 2; j++) {
            if (j % 2 == 0) cout << "*";
            else cout << char(i + 64);
        }
        cout << "\n";
    }
    return 0;
}

🧠 How It Works

1

Upper half: i = 1 .. n

Each row prints one repeated letter using char(i + 64) or equivalently char('A' + i - 1). Row 1 is only A; wider rows alternate that letter with * via cout.

Up
2

Inner stripe: j from 1 to 2i - 1

for (j = 1; j < i * 2; j++) visits an odd number of positions. Even j prints *; odd j prints the row letter. That produces B*B, C*C*C, … with stars between letters.

Alternating
3

Lower half: i = n-1 .. 1

After the peak row at i = n, a second outer loop counts i down with the same inner body so the diamond narrows again without printing the widest line twice.

Mirror
4

Why width is 2i - 1

Row index i needs i letters and i - 1 star gaps, hence 2i - 1 symbols. For n = 5 the middle line has nine tokens.

Peak
=

Diamond cost

Upper and lower halves each sum odd widths 1 + 3 + … + (2n-1) with the shared middle counted once in this split-loop style. Overall work is Θ(n²) for half-height n.

2

Variation — User Input (Half Height)

Lower loop starts at rows - 1 so the widest row prints once.

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

int main() {
    int rows, i, j;

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

    for (i = 1; i <= rows; ++i) {
        for (j = 1; j < i * 2; ++j) {
            if (j % 2 == 0) cout << "*";
            else cout << char('A' + i - 1);
        }
        cout << "\n";
    }

    for (i = rows - 1; i >= 1; --i) {
        for (j = 1; j < i * 2; ++j) {
            if (j % 2 == 0) cout << "*";
            else cout << char('A' + i - 1);
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Add leading spaces so the diamond is centered in the terminal
  • Swap * for - or another separator
  • Cap rows so 'A' + rows - 1 <= 'Z'

Avoid

  • Starting the lower loop at rows (duplicates the widest row)
  • Flipping the parity branches without also changing the intended pattern

Key Takeaways

1

Odd j prints the letter, even j prints *.

2

j < 2*i gives 2i - 1 symbols per row.

3

Mirror with i = n-1 .. 1 after 1 .. n.

4

O(n²) prints for half-height n.

❓ Frequently Asked Questions

In this code, even j prints *; odd j prints the row letter.
Only the upper loop runs once (printing A); the lower loop starts at 0 and does nothing.
It assumes ASCII. 'A' + i - 1 states the intent more clearly.
O(n²) for half-height n.

Explore More C++ Alphabet Patterns!

Two-phase loops (up then down) are the standard way to code diamonds without repeating the center line.

All Alphabet Patterns →
Did you know?

On row i the letter appears i times and * appears i - 1 times, for a total of 2i - 1 characters.

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