Right-Aligned Sequential Pyramid in C

What You’ll Learn
Like program 13, one counter k walks the alphabet, but each row is padded on the left so the growing run sits flush on the right.
Use a monospace font when running the program so %2c columns line up.
⭐ Pattern Output
Five rows (spacing from " " + %2c):
A
B C
D E F
G H I J
K L M N OComplete C Program ('A'–'E')
Same logic with int k = 65 and for (i = 65; i <= 69; i++) as in the reference.
#include <stdio.h>
int main() {
int i, j;
char k = 'A';
for (i = 'A'; i <= 'E'; ++i) {
for (j = 'E'; j >= 'A'; --j) {
if (j > i) {
printf(" ");
} else {
printf("%2c", k++);
}
}
printf("\n");
}
return 0;
}🧠 How It Works
k = 'A' streams the alphabet
Unlike a triangle that restarts each row, k keeps increasing. Every real letter print uses k++, so the pattern reads as one continuous A, B, C, … sequence laid into a grid.
Outer i sets how many letters appear
i is the row’s peak letter. Row A exposes one letter slot, row B two, up to five on row E. The rest of the fixed-width line is padded.
Inner j walks columns right to left
For each column index j from E down to A, if j > i you print two spaces (an empty %2c cell). Otherwise you print the next streamed letter with printf("%2c", k++) so columns line up.
Why the grid is five cells wide
You always iterate across the full letter range, so each row has five logical columns. Doubled spaces plus %2c make the staircase align the same way as the sample output on any screen width.
Letter count
Rows print 1+2+3+4+5 = 15 letters, ending at O. Padding loops add more writes, but time still scales as O(n²) for n letters in this fixed-grid style.
Variation — User Input
endChar = (char)('A' + rows - 1); inner j runs endChar down to 'A'.
#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 >= startChar; --j) {
if (j > i) {
printf(" ");
} else {
printf("%2c", k++);
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Tighter layout: one space +
%c(then adjust padding to match) - Print a leading space between pairs with
" "after%2cfor readability - Ensure
rowsso the last letter stays ≤'Z'
Avoid
- Mixing single-space padding with
%2cwithout checking alignment in your terminal - Resetting
keach row (you would repeatA,AB, …)
Key Takeaways
k++ only when a letter is printed; order follows the inner loop.
j > i reserves left columns; %2c fills right columns evenly.
Total letters for n rows from A: n(n+1)/2.
O(n²) inner steps for n rows.
❓ Frequently Asked Questions
j > i branch so every slot stays two columns wide.k and the row is right-justified in a fixed grid.Explore More C Alphabet Patterns!
Pairing a format width on letters with fixed-width padding is a simple way to grid-align console output.
Row i (as the rth letter from A) prints exactly r new characters; the running total after n rows is the nth triangular number.
12 people found this page helpful
