Inverted V-Shaped Alphabet Pattern in C

What You’ll Learn
Draw a letter V that opens toward the bottom: one A on the first row, then pairs B B, C C, and so on, with gaps growing in the middle. Two inner loops fill a fixed grid with spaces except on two diagonals.
Contrast with program 31 (normal V, wide at the top) and program 32 (filled palindrome pyramid).
⭐ Pattern Output
Five rows; each full line is 5 + 4 = 9 characters (leading and trailing spaces matter in the console):
A
B B
C C
D D
E EComplete C Program ('A'–'E')
First block: j from 69 down to 65. Second block: k from 66 through 69 only.
#include <stdio.h>
int main() {
int i, j, k;
for (i = 65; i <= 69; ++i) {
for (j = 69; j >= 65; --j) {
if (i == j) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = 66; k <= 69; ++k) {
if (i == k) {
printf("%c", k);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer i walks the alphabet upward
i is both the row index and the letter that should appear on the two arms for that row. Growing i widens the hollow inverted V until the bottom row shows the full span.
Left pass: j from top letter down to A
For each column code j, print the letter only if i == j; otherwise print a space. Because j moves from high to low, the visible letter slides left as i increases.
Right pass: k from B up to the top letter
The second loop mirrors the first on the right side. Starting k at B avoids a duplicate A on row A; for larger i both passes can emit the same letter, giving the paired columns in the output.
Why the middle looks empty
Most pairs (i, j) and (i, k) fail the equality test, so you print spaces. Only the two diagonal hits become letters, which leaves a growing gap between the arms.
Two prints per wide row
When i >= 'B', the same code point satisfies both i == j and i == k, so you see two letters on that row. Two full inner scans per outer row → O(n²) for n letters in the range.
Variation — User Input
endChar = (char)('A' + rows - 1); right loop uses k from startChar + 1 through endChar.
#include <stdio.h>
int main() {
int rows;
int i, j, k;
char startChar, endChar;
printf("Enter the number of rows: ");
scanf("%d", &rows);
startChar = 'A';
endChar = (char)('A' + rows - 1);
for (i = startChar; i <= endChar; ++i) {
for (j = endChar; j >= startChar; --j) {
if (i == j) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = startChar + 1; k <= endChar; ++k) {
if (i == k) {
printf("%c", k);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Rewrite with
'A'…'E'as character bounds fori,j, andk - Fill the interior between the legs for a solid downward triangle
- Stretch the V with double spaces in each loop body
Avoid
- Starting the second loop at
startCharunless you want twoAs on row one - Non-monospace output; alignment will look broken
Key Takeaways
Two conditional inner loops; letters appear only where indices match the row letter.
Right loop begins at startChar + 1 so the apex row is a single character.
Line width is 2n - 1 character prints for n letters (n left block + n - 1 right block).
O(n²) time for n rows.
❓ Frequently Asked Questions
B downward both hit the same letter, so the two arms move apart as i increases.9 × 5 = 45 characters, not 5² = 25. Big-O is still O(n²) because each row does Theta(n) work for n rows.A through the last letter.More Alphabet Patterns
Diagonal and V patterns are the same idea: choose which column indices may print on each row.
If the second loop started at 'A', row A would satisfy i == k twice across the two blocks and you would see two As unless you change the ranges.
12 people found this page helpful
