Decreasing & Increasing Alphabet Rows in C

What You’ll Learn
Each line is descending from the row letter down to B, then ascending from A to a cap chosen so the line always has five characters (for A…E). The cap is 'A' + 'E' - i, which matches 134 - i in ASCII.
Near kin: program 24 (palindrome triangle) uses a similar split around A.
⭐ Pattern Output
Adjacent %c, no spaces:
ABCDE
BABCD
CBABC
DCBAB
EDCBAASCII version (134 - i)
Same as k <= 65 + 69 - i for row index i.
#include <stdio.h>
int main() {
int i, j, k;
for (i = 65; i <= 69; ++i) {
for (j = i; j > 65; --j) {
printf("%c", j);
}
for (k = 65; k <= 134 - i; ++k) {
printf("%c", k);
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer i is the row anchor (ASCII)
i runs 65…69 (A…E). It defines the leftmost “high” letter on the row; the second loop fills the ascending tail that balances the line to fixed length.
Descending leg: j from i toward B
Using ASCII, for (j = i; j > 65; --j) prints i, then i-1, down to B. When i == 'A' the condition fails immediately, so the first row has no descending prefix.
Ascending leg: k up to 134 - i
The second loop walks k from 65 through 134 - i, which equals 65 + 69 - i ('A' + 'E' - i in ASCII). As i increases, the cap drops so the row stays five letters wide.
Constant row length
Descending count is i - 65 letters; ascending count is (134 - i) - 65 + 1 = 69 - i + 1 letters. Their sum is 69 - 65 + 1 = 5 for every row in this range.
Bow-tie pattern
Middle rows splice a descending prefix with an ascending tail, so they read almost palindromic. First and last rows are one-sided ramps. Two inner passes per row → O(n²) for n letters.
Character literals & user input
Use 'A' + endChar - i (not 2 * endChar - i) so the bound stays correct when i == 'A'.
#include <stdio.h>
int main() {
int rows;
int i, j, k;
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 = i; j > startChar; --j) {
printf("%c", j);
}
for (k = startChar; k <= (startChar + endChar - i); ++k) {
printf("%c", k);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Print spaces between letters for readability
- Generalize
startCharandendCharfrom input with validation - Derive the cap as
endChar - (i - startChar)after rewriting the sum
Avoid
2 * endChar - ias the upper limit whenstartCharis'A'(wrong for the first row)- Using
j >= 'A'in the first loop without removingAfrom the second
Key Takeaways
Upper bound for k: startChar + endChar - i (ASCII 134 - i for A…E).
First loop only emits down to B so A is not doubled.
Every row length equals endChar - startChar + 1.
O(n²) for n rows over an n-letter span.
❓ Frequently Asked Questions
i == 'A' and endChar == 'E', that expression overshoots past E. The correct symmetric cap is startChar + endChar - i.65 + 69, i.e. 'A' + 'E', so 134 - i is just 'A' + 'E' - i written as integers.Explore More C Alphabet Patterns!
Encoding the ascending bound as start + end - row keeps row length stable without hard-coding magic numbers beyond your letter range.
Letters printed in the second loop count (A + E - i) - A + 1 = E - i + 1; together with i - A letters from the first loop you always get E - A + 1 per row.
12 people found this page helpful
