Right-Aligned Alphabet Pyramid in C

What You’ll Learn
Pad with ordinary spaces, then print A … i using %2c. Same family as program 22 (grid + %2c), but here each row repeats from A and uses one space per padding step.
Use a monospace font so alignment holds.
⭐ Pattern Output
Five rows; bottom row is the widest:
A
A B
A B C
A B C D
A B C D EComplete C Program ('A'–'E')
Equivalent to loops on 65…69 with j > i for spaces.
#include <stdio.h>
int main() {
int i, j, k;
for (i = 'A'; i <= 'E'; ++i) {
for (j = 'E'; j > i; --j) {
printf(" ");
}
for (k = 'A'; k <= i; ++k) {
printf("%2c", k);
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer i grows the letter run
i walks A…E. It marks how many letters belong on the row: row A prints one letter, row B prints two, and so on, while staying right-aligned.
Leading spaces from high columns
Scan j from E down to i + 1. Each step prints one ordinary space. The count is 'E' - i, which shrinks as the letter block grows, pushing the ramp to the right edge.
Letters A through i with %2c
The second loop prints every letter from the start of the alphabet up to the row peak. Width-two formatting keeps single letters aligned in columns so the staircase reads clearly on narrow viewports.
Constant visual width
Spaces plus doubled letters keep the right margin fixed: the top row is mostly padding, the bottom row uses five wide letter slots. Total non-newline output per row stays predictable for layout.
Example row
For i = 'C' you print two padding spaces, then %2c for A, B, C. Five rows × O(n) inner work → O(n²) overall for n letters.
Variation — User Input
endChar = (char)('A' + rows - 1); padding loop uses j > i with j starting at endChar.
#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 = endChar; j > i; --j) {
printf(" ");
}
for (k = startChar; k <= i; ++k) {
printf("%2c", k);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Left-align: drop the space loop
- Center: split padding roughly before and after the letter run
- Match padding width to
%3cif you widen letters
Avoid
- Mixing double-space padding with
%2cwithout rechecking the grid - Expecting perfect alignment in a proportional font
Key Takeaways
Padding count per row: endChar - i single spaces.
Letter count per row: i - startChar + 1.
%2c pairs with monospace output for even columns.
O(n²) work for n rows.
❓ Frequently Asked Questions
j is strictly past the current row letter. When j == i, stop so the next character printed is the start of the letter run.%c. Here each row grows A…i and is pushed right with spaces and %2c.Explore More C Alphabet Patterns!
Single-space padding plus wide letter fields is a simple recipe for a right-justified growing triangle.
Each step down removes one padding iteration and adds one letter to the run; with %2c and a monospace font, the longest row defines the right margin.
12 people found this page helpful
