Inverted V-Shaped Alphabet Pattern in C++

What You’ll Learn
This pattern draws an inverted V: one A at the top, then pairs like B B, C C, and so on, widening as you go down.
We scan a fixed-width row and print letters only where the row letter matches the current column index.
⭐ Pattern Output
Output for 5 rows:
A
B B
C C
D D
E EC++ Program (Reference Logic)
Left scan from E to A, then right scan from B to E.
#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";
}
return 0;
}🧠 How It Works
Row letter i
for (char i = 'A'; i <= 'E'; ++i) walks each row. The current letter is the one that may appear on both legs (except the apex row, which only prints once).
Left block j (E..A)
for (char j = 'E'; j >= 'A'; --j) with cout << (i == j ? j : ' ') draws the descending diagonal: only the column where j matches i shows a letter; the rest are spaces.
Right block k (B..E)
for (char k = 'B'; k <= 'E'; ++k) mirrors the rule on the ascending side. Starting at B skips a duplicate A on the first row while still forming the widening V.
Symmetry & newline
Both inner loops use the same predicate (i == column) so legs stay aligned. cout << "\n" after each pair finishes the row before the next i.
Opening downward
Each row adds width between the legs. Five rows with two O(n) passes per row → O(n²) time and O(1) extra space beyond the stream.
Variation — User Input
For rows, set endChar = 'A' + rows - 1 and run the same two scans; the right scan starts at startChar + 1.
#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 >= startChar; --j) {
cout << (i == j ? j : ' ');
}
for (char k = char(startChar + 1); k <= endChar; ++k) {
cout << (i == k ? k : ' ');
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Stretch the shape by printing two spaces instead of one
- Fill the inside region for a solid triangle
- Keep
rows <= 26for A..Z only
Avoid
- Starting the right scan at
Aif you want a single apex - Proportional fonts when evaluating alignment
Key Takeaways
Two diagonal checks pick out the two legs.
Right block starts at B so the first row prints only one A.
Width is 2n - 1 for n letters.
O(n²) time for n rows.
❓ Frequently Asked Questions
A doesn’t print twice. From row B onward, both legs can match the same letter.Explore More C++ Alphabet Patterns!
Once you can pick diagonal positions with equality checks, you can draw many shapes in a console grid.
If the right block started at A, the top row would print two As (one on each side) unless you changed the ranges.
12 people found this page helpful
