V-Shaped Alphabet Pattern in C

What You’ll Learn
Print letters only where the row letter i matches the column letter j or k; everywhere else print a space. The two diagonals form a V (like the letter): wide at the top with A on both sides, meeting at the bottom row’s vertex letter.
This is not the same as program 33 (inverted V, wide at the bottom). Related idea: program 30 stitches two runs on one row; here each leg is mostly blanks.
⭐ Pattern Output
Five rows, 5 + 4 = 9 characters wide (trailing spaces on the last row may be invisible in some viewers):
A A
B B
C C
D D
EComplete C Program ('A'–'E')
ASCII 65…69; second loop ends at 65 with start 68 ('D').
#include <stdio.h>
int main() {
int i, j, k;
for (i = 65; i <= 69; ++i) {
for (j = 65; j <= 69; ++j) {
if (i == j) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = 68; k >= 65; --k) {
if (i == k) {
printf("%c", k);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Row index i
Also the letter code for that row: A through E.
Left leg i == j
j scans the alphabet left to right; the match hits the descending diagonal of the combined picture.
Right leg i == k
k runs D ↓ A; same row letter, mirrored positions.
Vertex row
When i == E, only the left loop can match; the right loop prints spaces only—the tip of the V is a single letter.
Spacing
Leading spaces before the first letter grow row by row; the two arms move toward the center.
Variation — User Input
endChar = (char)('A' + rows - 1); right loop k from endChar - 1 down to startChar.
#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 = startChar; j <= endChar; ++j) {
if (i == j) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = endChar - 1; k >= startChar; --k) {
if (i == k) {
printf("%c", k);
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Double the horizontal gap with
" "instead of one space - Star version: print
*on diagonals with the samei == jtests in a square matrix - Odd-sized alphabet blocks keep one center letter on the last row
Avoid
- Running
kthroughendCharunless you redesign width (can duplicate the bottom vertex) - Proportional fonts—alignment disappears
Key Takeaways
Two scans per row; only equality tests pick out diagonal cells.
Right loop length n - 1 keeps total width 2n - 1.
Same i is compared to both j and k; the printed character equals i when matched.
O(n²) for n rows.
❓ Frequently Asked Questions
i == 'E' you would print another E in the right block, widening the line and breaking the single vertex at the tip of the V. Starting at endChar - 1 matches the reference layout.5² = 25. Complexity is still O(n²) when the alphabet has n letters because each row does Theta(n) work.i equality rules.A to the top letter.Explore More C Alphabet Patterns!
Diagonal patterns are just equality checks between row and column indices dressed as letters.
On row i, at most two cells print letters—one from each inner loop—except you only see one on the bottom row because the right scan never reaches i.
12 people found this page helpful
