A symmetric decreasing alphabet square prints concentric letter layers: the top row is all Es, then borders step down until the center row reads E D C B A B C D E.
Remember
Rule: for floor i from top down to A,
left scan top..A, right scan B..top,
print (j > i ? j : i)
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E ← top = 'E'
Each cell chooses the larger of the column letter and the row floor. The right half starts at B so the center A is not duplicated. Program 29 reuses this row logic to build a full diamond.
Approach
How to Solve It
Two ways to emit the same shape — inline if (j > i) in both halves, or a small helper that applies the floor rule once.
Method
Idea
Best for
Two mirrored scans
Left k..A, right B..k; same j > i rule
Learning, interviews, exams
Helper print_cell
One function applies the floor rule for both halves
Cleaner demos once the rule clicks
Pseudocode
Pseudocode
k = topLetter
for i from k down to 'A':
for j from k down to 'A':
print (j > i ? j : i) and a space
for j from 'B' to k:
print (j > i ? j : i) and a space
print newline
Cheat sheet
Goal
Pattern
Lower the floor
for (i = k; i >= 'A'; i--)
Left half
for (j = k; j >= 'A'; j--)
Right half
for (j = 'B'; j <= k; j++)
Cell rule
printf("%c ", j > i ? j : i);
End the row
printf("\n");
Row width
2 * (k - 'A') + 1 cells
Printing Letters vs Starting a New Line
API
Effect
Use for
printf("%c ", ch)
Letter + space; stays on the line
Each cell
printf("\n")
Ends the current line
After both halves
Try it
Live Preview
Change the letter count (top = A + n - 1) and the square updates instantly — capped at 8 for readability.
Whole numbers from 1 to 8. With n = 5, top is E and you get the classic sample.
Live result5 rows · 45 cells
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
Trace
Worked Walkthrough — top = 'C'
Smaller board (n = 3, width 5) so each floor is easy to check by hand.
Floor i
Left (C..A)
Right (B..C)
Printed row
'C'
C C C
C C
C C C C C
'B'
C B B
B C
C B B B C
'A'
C B A
B C
C B A B C
On floor 'B', columns with j > 'B' print the border C; interior cells print B.
Code
C Programs
Three complete programs: fixed top letter, scanf input, and a helper-function rewrite. Use View Output to reveal sample results.
Example 1 — Fixed top at 'E'
Hard-coded top — two mirrored scans with the same j > i check.
C
#include <stdio.h>
int main(void)
{
char k = 'E';
char i, j;
for (i = k; i >= 'A'; i--)
{
for (j = k; j >= 'A'; j--)
{
if (j > i)
printf("%c ", j);
else
printf("%c ", i);
}
for (j = 'B'; j <= k; j++)
{
if (j > i)
printf("%c ", j);
else
printf("%c ", i);
}
printf("\n");
}
return 0;
}
Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
How It Works
1. Outer loop lowers the floor.i runs from 'E' down to 'A' — one row per floor.
2. Left half scans down to A. For each column j, print j if j > i, else print the floor i.
3. Right half mirrors from B. Starting at 'B' skips a second center A and completes the symmetry.
4. Break the line.printf("\n") after both halves starts the next lower floor.
Example 2 — User Input Version
Read the top letter at runtime with scanf. Prefer a leading space in the format and validate A–Z.
C
#include <stdio.h>
int main(void)
{
char k, i, j;
printf("Enter top letter (like E): ");
scanf(" %c", &k);
for (i = k; i >= 'A'; i--)
{
for (j = k; j >= 'A'; j--)
{
if (j > i)
printf("%c ", j);
else
printf("%c ", i);
}
for (j = 'B'; j <= k; j++)
{
if (j > i)
printf("%c ", j);
else
printf("%c ", i);
}
printf("\n");
}
return 0;
}
Output (when user enters C)
Enter top letter (like E): C
C C C C C
C B B B C
C B A B C
How It Works
1. Prompt and read.scanf(" %c", &k) — the leading space skips leftover newlines.
2. Same square core. Only the top letter changes; the floor rule and both halves stay identical.
3. Safer input tip. Reject lowercase or non-letters:
Safer input
if (scanf(" %c", &k) != 1 || k < 'A' || k > 'Z')
{
printf("Enter an uppercase letter A–Z.\n");
return 1;
}
Example 3 — Helper function
Same shape with one print_cell so both halves stay thin and the floor rule lives in one place.
C
#include <stdio.h>
void print_cell(char j, char i)
{
if (j > i)
printf("%c ", j);
else
printf("%c ", i);
}
int main(void)
{
char k = 'E';
char i, j;
for (i = k; i >= 'A'; i--)
{
for (j = k; j >= 'A'; j--)
print_cell(j, i);
for (j = 'B'; j <= k; j++)
print_cell(j, i);
printf("\n");
}
return 0;
}
Output
E E E E E E E E E
E D D D D D D D E
E D C C C C C D E
E D C B B B C D E
E D C B A B C D E
How It Works
1. Centralize the rule.print_cell encodes j > i ? j : i once.
2. Keep the same scans. Left still walks k..A; right still walks B..k.
3. Learn the inline version first. Use Examples 1–2 when you need every branch visible; treat the helper as a polish shortcut.
Edge Cases & Pitfalls
Check these before calling the solution done.
Right from A
Duplicated center
Starting the right half at 'A' prints A twice. Always start at 'B'.
Wrong compare
Broken borders
Use j > i (strict). j >= i shifts which cells belong to the floor vs the border.
\n early
Broken rows
Call printf("\n") only after both halves finish.
No space
Letters glued together
Use printf("%c ", ch) (letter + space) so the square stays readable.
top = 'A'
Single A
One row with a single A — left prints once; the right half never enters.
scanf
Leading space in format
Use scanf(" %c", &k) so a leftover newline is not read as the letter.
Analysis
Time and Space Complexity
Program
Time
Extra space
Inline scans (Examples 1–2)
O(n²)
O(1)
Helper (Example 3)
O(n²)
O(1)
For n letters there are n rows and each row prints 2n - 1 cells — quadratic in n.
Remember
Key Takeaways
Rule: print j > i ? j : i on mirrored left and right scans.
Start right at B: that keeps a single center A on the last row.
Break the row: call printf("\n") only after both halves.
Complexity:O(n²) time; O(1) extra space.
One line: for each floor i, scan left k..A and right B..k, printing j > i ? j : i, then printf("\n").
Frequently Asked Questions
It prints the border letters when the column letter j is above the current row floor i; otherwise it prints i. That builds higher-letter borders with a flat interior.
The first loop scans E down to A. The second scans B up to E so the middle A is printed once and the row is mirrored.
For letters A..k, width is (k-A+1) + (k-A) = 2*(k-A)+1. For A..E, width is 9.
Starting at A would print A again and duplicate the center. Starting at B mirrors the left half cleanly.
printf("%c ", ch) prints a letter plus a trailing space and stays on the same line. printf("\n") ends the current line after both halves.
O(n²) for n letters because there are n rows and each row prints O(n) cells.
Use scanf(" %c", &top) with a leading space, require A–Z, and reject non-letters.
Program 29 reuses the same row logic while descending to A, then mirrors upward from B so you get a full reverse-centered diamond without duplicating the center row.
🤔
Did you know?
Fix k at the top letter (E). Outer loop i goes from k down to A. Left half scans j = k..A; right half scans j = B..k so A appears once in the middle. Each cell prints j when j > i, otherwise i.