Diamond-Shaped Alphabet Pattern in C++

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:
A
B B
C C
D D
E E
D D
C C
B B
AC++ Program (Reference Logic)
Two phases with identical inner loops; second phase starts at D to avoid duplicating the E row.
#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
Top outer loop (grow the V)
i runs 65…69 (A…E). Each larger i places the letter farther out on both diagonals so the hollow shape opens toward the widest E row.
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).
Bottom outer loop (shrink the V)
The second part runs i from 68 down to 65 (D…A) with the same inner loops pasted again, rebuilding the arms in reverse so the diamond closes symmetrically.
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.
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.
Variation — User Input
Enter rows (half height). Bottom half starts at endChar - 1 to avoid duplicating the center row.
#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
rowswithin 26 for A..Z
Avoid
- Starting the bottom half at
endChar(duplicates the center row) - Proportional fonts when you want perfect alignment
Key Takeaways
Diamond = top half + mirrored bottom half.
Start bottom half at endChar - 1 to avoid a duplicate center.
Rows and width both scale as \(2n-1\).
O(n²) time for n letters.
❓ Frequently Asked Questions
endChar - 1, skipping the already-printed endChar row.Explore More Pattern Programs!
Once you can mirror a shape vertically, you can create diamonds, hourglasses, and many other symmetric patterns.
The same row-printing logic is reused for both halves—only the range of the outer loop changes to mirror the output.
12 people found this page helpful
