Reverse Alphabet, Diagonal *

What You’ll Learn
Five rows, each like EDCBA but with exactly one * where the row letter meets the column letter (i == j).
The star slides from the right (EDCB*) to the left (*DCBA) as i increases. Compare program 16 (centered pyramid, no diagonal).
⭐ Pattern Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBAComplete C Program (A–E)
Same logic as for (i = 65; i <= 69; i++) and j from 69 to 65.
#include <stdio.h>
int main() {
int i, j;
for (i = 'A'; i <= 'E'; ++i) {
for (j = 'E'; j >= 'A'; --j) {
if (i == j) {
printf("*");
} else {
printf("%c", j);
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer i picks the diagonal cell
i walks each row label from A up to E. That value is compared to the inner index j so exactly one position per row prints * instead of a letter.
Inner j scans the reverse alphabet
For each row, j counts from the top letter (E here) down to A. Without the star rule you would simply print the descending sequence across the row.
Diagonal swap: if (i == j)
When the row letter equals the column letter, emit * instead of printf("%c", j). That carves a diagonal of stars through the reverse-letter square.
First row walkthrough
For i = 'A', j still scans E ↓ A. Letters print until j reaches 'A', which equals i, so that slot is * → EDCB*.
Full square
Five rows with five inner iterations each → 25 writes. For an n × n letter range the same pattern is O(n²).
Variation — User Input
endChar = 'A' + rows - 1; loops use startChar and endChar.
#include <stdio.h>
int main() {
int rows;
int i, j;
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("*");
} else {
printf("%c", j);
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Swap
*for#or another marker - Mirror the inner loop to get
ABCDEwith a diagonal hole - Cap
rowssoendChar <= 'Z'
Avoid
- Comparing
iandjas different types without promotion rules in mind (here both behave asintin expressions) - Using
rowslarger than 26 without changing the alphabet range
Key Takeaways
Descending j gives reverse letters on each line.
i == j picks one diagonal cell per row.
Generalizes by setting endChar from rows.
O(n²) for an n×n letter square.
❓ Frequently Asked Questions
j counts down), the stars sit on one anti-diagonal of the 5×5 letter matrix.i + j == 'A' + 'E' (adjust ends for your range) so the match moves along the opposite slant.Explore More C Alphabet Patterns!
Pairing indices with character codes is a compact way to mark one cell per row.
Here i and j are both promoted as integers in comparisons and printf("%c", j); the pattern still works if you declare them as char in C as long as you stay within a single-byte range.
12 people found this page helpful
