An inverted V-shaped alphabet pattern prints a single A at the top, then letter pairs that open downward (B B, C C, …) with spaces everywhere else.
Remember
Rule: left n..0 when i==j; right 1..n when i==k
A
B B
C C
D D
E E ← tip A once (end = 'E')
The right scan starts at 1 (not 0) so the tip A prints once. It is the upside-down cousin of Program 31; Program 34 stacks this shape with its mirror to make a diamond.
Approach
How to Solve It
Two ways to emit the same inverted V — inline diagonal checks, or a shared print_cell helper.
Method
Idea
Best for
Two scans inline
Left n..0, right 1..n, print when row == col
Learning, interviews, exams
print_cell helper
One function owns the diagonal rule for both legs
Cleaner demos once the tip skip clicks
Pseudocode
Pseudocode
n = end - 'A'
for i from 0 to n:
for j from n down to 0:
print (i == j ? alpha[j] : ' ')
for k from 1 to n:
print (i == k ? alpha[k] : ' ')
print newline
Cheat sheet
Goal
Pattern
End index
int n = end - 'A'; (4 for E)
Rows
for (i = 0; i <= n; i++)
Left leg
for (j = n; j >= 0; j--) printf("%c", i == j ? alpha[j] : ' ');
Right leg
for (k = 1; k <= n; k++) printf("%c", i == k ? alpha[k] : ' ');
Change the end letter and the inverted V updates instantly — width is always 2n + 1, with a single A on the first row.
One letter from A to F. Tap a chip or type a letter — use a monospace view for alignment.
Live resultEnd E · 5 rows · width 9
A
B B
C C
D D
E E
Trace
Worked Walkthrough — End = E (n = 4)
Trace each row’s diagonal hits and the resulting 9-column line.
i
Left hit
Right hit
Printed row
0
A at j=0
(none)
A
1
B at j=1
B at k=1
B B
2
C at j=2
C at k=2
C C
3
D at j=3
D at k=3
D D
4
E at j=4
E at k=4
E E
Width is always 2×4 + 1 = 9. The tip row has no right-leg match because k never equals 0.
Code
C Programs
Three complete programs: fixed A–E, end-letter input with scanf, and a helper-function rewrite. Use View Output to reveal sample results.
Example 1 — Fixed A–E
Left scan runs from E..A, then right scan runs from B..E.
c
#include <stdio.h>
int main(void) {
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i, j, k;
for (i = 0; i <= 4; i++) {
for (j = 4; j >= 0; j--) {
if (i == j)
printf("%c", alpha[j]);
else
printf(" ");
}
for (k = 1; k <= 4; k++) {
if (i == k)
printf("%c", alpha[k]);
else
printf(" ");
}
printf("\n");
}
return 0;
}
Output
A
B B
C C
D D
E E
How It Works
1. Outer loop picks the row.i runs from 0 to 4 (letters A…E).
2. Left leg — reverse scan. Scan j = 4..0; print the letter only when i == j, else a space.
3. Right leg — skip A. Scan k = 1..4; print when i == k. Starting at 1 skips a second tip A.
4. Tip row. When i = 0, only the left scan can print A.
Example 2 — End Letter Input
The left scan is end..A and the right scan is B..end. Check scanf and validate a single A–Z character in real apps.
c
#include <stdio.h>
int main(void) {
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char end;
int n, i, j, k;
printf("Enter end letter (like E): ");
scanf(" %c", &end);
n = end - 'A';
for (i = 0; i <= n; i++) {
for (j = n; j >= 0; j--)
printf("%c", i == j ? alpha[j] : ' ');
for (k = 1; k <= n; k++)
printf("%c", i == k ? alpha[k] : ' ');
printf("\n");
}
return 0;
}
Output (when user enters C)
Enter end letter (like E): C
A
B B
C C
How It Works
1. Scale with n.n = end - 'A' drives both scans. For end = C, width is 2×2 + 1 = 5.
2. Same tip rule. Right loop still starts at 1 so the tip is a single A.
3. Safer input tip. Prefer:
Safer input
if (scanf(" %c", &end) != 1 || end < 'A' || end > 'Z') {
printf("Enter one letter from A to Z.\n");
return 1;
}
Example 3 — Helper Function
Often clearer: one function applies the diagonal rule so left and right loops stay thin.
c
#include <stdio.h>
void print_cell(char alpha[], int row, int col) {
printf("%c", row == col ? alpha[col] : ' ');
}
int main(void) {
char alpha[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int n = 4;
int i, j, k;
for (i = 0; i <= n; i++) {
for (j = n; j >= 0; j--)
print_cell(alpha, i, j);
for (k = 1; k <= n; k++)
print_cell(alpha, i, k);
printf("\n");
}
return 0;
}
Output
A
B B
C C
D D
E E
How It Works
1. print_cell owns the rule.row == col lives in one place for both legs.
2. Tip still skipped on the right. The right loop starts at 1 even with the helper.
Edge Cases & Pitfalls
Check these before calling the solution done.
k = 0
Double tip A
If the right scan includes index 0, the first row prints A twice. Keep for (k = 1; k <= n; k++).
No spaces
Collapsed shape
Writing only letters packs both legs together. Non-hits must print ' '.
printf("\n") inside
Column of cells
If printf("\n") is inside either scan, each cell lands on its own line. Print cells with %c; break the line only after both scans.
end = A
Single A
When n = 0, left prints A and the right loop never runs. A good sanity check.
Left 0..n
Wrong left direction
Scanning left 0..n instead of n..0 still hits the diagonal but shifts column order — keep the reverse scan for the classic layout.
scanf
Leading space in format
Use scanf(" %c", &end) so a leftover newline does not become your letter. Still validate A–Z.
Analysis
Time and Space Complexity
Program
Time
Extra space
Two scans inline (Examples 1–2)
O(n²)
O(1) (plus the fixed alphabet table)
Helper function (Example 3)
O(n²)
O(1)
About n + 1 rows × 2n + 1 characters printed per row — still quadratic in n.
Remember
Key Takeaways
Rule: print a letter only when row index equals column index.
Single tip A: right scan starts at 1, not 0.
Spaces keep the shape: non-hits print ' '; break the row with printf("\n") after both scans.
Complexity:O(n²) time; O(1) extra space.
One line: for each row, scan left n..0 and right 1..n, printing a letter only on the diagonal.
Frequently Asked Questions
Because the right block starts from B, so it never matches i = 0 (A). Only the left block prints A on the first row.
Width is 2n+1: n+1 columns from the left block and n columns from the right block. For A–E, n is 4 and width is 9.
Program 31 is wide at the top and has a single bottom vertex. Program 33 has a single A at the top and widens downward with pairs like B B, C C.
O(n²) because there are n+1 rows and each row scans O(n) positions across both blocks.
Spaces keep column alignment so the two diagonals open into a visible inverted V in a monospace console.
Check scanf(" %c", &end) == 1, require a single A–Z character, and reject invalid input.
Yes. Set n = end − 'A', scan j from n down to 0 on the left, and scan k from 1 to n on the right.
Program 34 reuses this inverted-V row logic for A..E, then mirrors D..A downward to close a full diamond without repeating the widest E row.
🤔
Did you know?
Two blocks per row. Left block scans E down to A and prints only when i == j. Right block scans B up to E and prints only when i == k. Because the right block never visits A, the first row prints a single A, while later rows print the same letter twice and the shape opens downward.