Sequential Decreasing Alphabet Triangle in C

What You’ll Learn
Shrink each row by one slot while a single k walks the alphabet without resetting—like program 22, but left-aligned and with shorter rows each time.
Prefer a monospace font so %2c columns align.
⭐ Pattern Output
Five rows, fifteen letters total (A…O):
A B C D E
F G H I
J K L
M N
OComplete C Program ('A'–'E')
Character literals; inner index j only controls how many times k++ runs.
#include <stdio.h>
int main() {
int i, j;
char k = 'A';
for (i = 'A'; i <= 'E'; ++i) {
for (j = 'E'; j >= i; --j) {
printf("%2c", k++);
}
printf("\n");
}
return 0;
}🧠 How It Works
k walks forward through the alphabet
Initialize k = 'A' once. Each time you actually print a letter you use printf("%2c", k++), so the triangle is filled with a single increasing sequence rather than restarting at A every row.
Outer i picks the row’s low letter
i runs A…E. It becomes the smallest letter that may appear on that row; letters below i in the alphabet are skipped by the inner loop bound.
Inner j from E down to i
Counting down from the top letter to the row floor gives a shorter slice each time i rises. The iteration count is 'E' - i + 1, which is 5, 4, 3, 2, 1 for this sample.
%2c for alignment
A width-two conversion adds leading space before single letters so the right-aligned triangle matches the tutorial diagram and stays readable on small screens when you scroll horizontally if needed.
Triangular total
Letter prints sum to 5+4+3+2+1 = 15, the fifth triangular number. Nested loops yield O(n²) character output for n rows in this style.
Variation — User Input
endChar = (char)('A' + rows - 1); inner j runs endChar down to i.
#include <stdio.h>
int main() {
int rows;
int i, j;
char k = 'A';
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 >= i; --j) {
printf("%2c", k++);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Widen columns with
%3cor use"%c "if you prefer explicit gaps - Start at
'a'for lowercase - Keep
rowssmall enough thatknever passes'Z'
Avoid
- Resetting
keach row (you would repeatA B C D Eevery line) - Choosing
rowsso required letters exceed your alphabet range
Key Takeaways
Inner loop count per row: endChar - i + 1 (with endChar = 'E' in the fixed example).
Total characters for n rows from a fresh A: n(n+1)/2.
j can be ignored in the body; it only schedules how many k++ prints occur.
O(n²) characters for n rows.
❓ Frequently Asked Questions
A again.for (j = 0; j < endChar - i + 1; j++)). Descending j matches the fixed E…i style from the ASCII version.%c. Here each row is a straight segment of the global k stream with %2c spacing.Explore More C Alphabet Patterns!
Using the inner index only as a repetition count while a separate variable supplies the symbol is a common pattern idiom.
For n rows with last row letter L, you need L - 'A' + 1 = n(n+1)/2 letters from A; for n = 5 that ends at 'O'.
12 people found this page helpful
