Symmetric Decreasing Alphabet Square in C

What You’ll Learn
Two passes per row share the same rule: print the larger of j and i when j > i, else print i. That fills a square whose “floor” drops row by row until A sits in the center of the last line, flanked by B C D E.
Compare program 24 (palindrome triangle) and program 27 (right-aligned growing rows).
⭐ Pattern Output
Five rows, nine letters per row (with a space after each %c in the program):
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 EComplete C Program ('A'–'E')
char k = 'E'; outer i from k down to 'A'.
#include <stdio.h>
int main() {
int i, j;
char k = 'E';
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;
}🧠 How It Works
i = row floor
Starts at E; each row the plateau letter drops until A on the last line.
Left half k ↓ A
When j > i, the visible letter follows j (outer shell); otherwise the row floor i fills the interior.
Right half B ↑ k
Same test; starting at B skips a second A after the middle A from the first loop.
%c
Trailing space after each letter matches the reference output spacing.
Symmetry
Each row reads the same left-to-right as right-to-left.
Variation — User Input
endChar = (char)('A' + rows - 1) assigns k; second loop starts at startChar + 1.
#include <stdio.h>
int main() {
int rows;
int i, j;
char k;
char startChar, endChar;
printf("Enter the number of rows: ");
scanf("%d", &rows);
startChar = 'A';
endChar = (char)('A' + rows - 1);
k = endChar;
for (i = k; i >= startChar; --i) {
for (j = k; j >= startChar; --j) {
if (j > i) {
printf("%c ", j);
} else {
printf("%c ", i);
}
}
for (j = startChar + 1; j <= k; ++j) {
if (j > i) {
printf("%c ", j);
} else {
printf("%c ", i);
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Remove the trailing space in
printfand rely on%2cif you want grid alignment - Try a larger
endCharand confirm the row width2(k - A) + 1 - Draw the same shape with a single loop over column index and mirror math
Avoid
- Starting the second loop at
'A'(would printAtwice on the last row) - Confusing row index
iwith column indexjwhen tracingj > i
Key Takeaways
The same if (j > i) rule is reused in both inner loops for a consistent shell and core.
Second loop begins at B so the center A appears only once.
Row width is 2 × (k - 'A') + 1 symbols (nine for A…E).
O(n²) output for n letters from A to the top row letter.
❓ Frequently Asked Questions
j is still above the current floor i, you show the “wall” letter j. At and below the floor you show i.n.j with a single midpoint rule; the two-loop version matches the reference and keeps the mirror explicit.Explore More C Alphabet Patterns!
Reusing one comparison in two sweeps is an easy way to enforce symmetry without manual string reversal.
The bottom row spells the full palindrome E D C B A B C D E: the first loop contributes down to A, the second continues upward from B.
12 people found this page helpful
