V-Shaped Alphabet Pattern in C++

What You’ll Learn
This pattern prints letters only on the two diagonals that form a V. Everywhere else, it prints spaces.
The last row prints a single vertex letter (E), because the right diagonal intentionally skips the last letter.
⭐ Pattern Output
Output for 5 letters (width \(2n-1 = 9\)):
A A
B B
C C
D D
EC++ Program (Reference Logic)
Two scans per row: one left-to-right, one right-to-left (starting from D).
#include <iostream>
using namespace std;
int main() {
int i, j, k;
for (i = 65; i <= 69; i++) {
for (j = 65; j <= 69; j++) {
if (i == j) cout << char(j);
else cout << " ";
}
for (k = 68; k >= 65; k--) {
if (i == k) cout << char(k);
else cout << " ";
}
cout << "\n";
}
return 0;
}🧠 How It Works
Outer i (65–69)
for (i = 65; i <= 69; i++) walks each row; i is the ASCII code of the letter that should appear on the two legs for that row (and only once at the apex).
Left scan j
for (j = 65; j <= 69; j++): if i == j, cout << char(j); else a space. Exactly one column matches per row, forming the downward leg from the left.
Right scan k from 68 down
for (k = 68; k >= 65; k--) uses the same i == k rule. Starting at 68 ('D') skips a second 'E' on the bottom row while still drawing the inward leg.
Newline & width
After both passes, cout << "\n" ends the row. Each line has a fixed character count (five left slots plus four right), so wide output scrolls horizontally on small screens via the shared layout styles.
Converging V
As i increases, the matching columns move toward the center. Five rows × two O(n) inner loops → O(n²) time, O(1) extra space.
Variation — User Input
Generalize to any rows (A..endChar). The right scan starts at endChar - 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 = startChar; j <= endChar; ++j) {
cout << (i == j ? j : ' ');
}
for (char k = char(endChar - 1); k >= startChar; --k) {
cout << (i == k ? k : ' ');
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Double the horizontal gap by printing two spaces instead of one
- Star version: print
*on diagonals using the same equality checks - Keep
rowswithin 26 if you want only letters A..Z
Avoid
- Including
endCharin the right scan unless you want a double vertex - Using a proportional font when you care about alignment
Key Takeaways
Only diagonal positions print letters; everything else is spaces.
Right scan is length n - 1 so width is 2n - 1.
Bottom row prints a single vertex letter.
O(n²) time for n rows.
❓ Frequently Asked Questions
E twice (left and right), changing the intended V-shape.Explore More C++ Alphabet Patterns!
Diagonal patterns are just equality checks between row and column indices.
On each row, at most two cells print letters (one per leg)—except the last row prints only one because the right scan does not include the last letter.
12 people found this page helpful
