Diamond Alphabet & Stars in C++

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:
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
AComplete C++ Program (Half Height 5)
Odd j prints the row letter, even j prints *. Upper half prints 1..5, lower half prints 4..1.
#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
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.
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.
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.
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.
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.
Variation — User Input (Half Height)
Lower loop starts at rows - 1 so the widest row prints once.
#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
rowsso'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
Odd j prints the letter, even j prints *.
j < 2*i gives 2i - 1 symbols per row.
Mirror with i = n-1 .. 1 after 1 .. n.
O(n²) prints for half-height n.
❓ Frequently Asked Questions
j prints *; odd j prints the row letter.A); the lower loop starts at 0 and does nothing.'A' + i - 1 states the intent more clearly.Explore More C++ Alphabet Patterns!
Two-phase loops (up then down) are the standard way to code diamonds without repeating the center line.
On row i the letter appears i times and * appears i - 1 times, for a total of 2i - 1 characters.
12 people found this page helpful
