Right-Aligned Reverse Pyramid in C

What You’ll Learn
Each row is a reverse suffix A, BA, CBA, … padded on the left so the letters line up on the right in a column of width five.
Same j > i idea as the left half of program 19, without the second mirror loop.
⭐ Pattern Output
Monospace (leading spaces matter):
A
BA
CBA
DCBA
EDCBAComplete C Program ('A'–'E')
Outer i is the row peak; inner j sweeps the full alphabet slice once per row.
#include <stdio.h>
int main() {
int i, j;
for (i = 'A'; i <= 'E'; ++i) {
for (j = 'E'; j >= 'A'; --j) {
if (j > i) {
printf(" ");
} else {
printf("%c", j);
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer i sets the visible tail
i runs A…E. On each row, letters from i down to A will eventually be printed right-aligned; values above i are suppressed as spaces first.
Inner j scans 'E' ↓ 'A'
Every row performs the same five-column scan. That keeps the grid width fixed so right alignment is easy: you always decide “space or letter” at each column index.
Leading spaces when j > i
If the column letter j is still “above” the row’s start letter i, that letter has not appeared on this row yet, so you print a space. Padding shrinks as i moves toward E.
Letters when j <= i
Once j reaches the row threshold, each step prints the descending letter j. You get a contiguous suffix i, i-1, … A hugging the right margin.
Grid cost
Five columns × five rows = 25 character decisions. For a span of n letters you scan n positions on each of n rows → O(n²).
Variation — User Input
endChar replaces 'E' in the inner loop bound.
#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 (j > i) {
printf(" ");
} else {
printf("%c", j);
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Separate loop: print
endChar - ispaces, then a simplejfromidown tostartChar - Flip to left-aligned forward triangle by scanning
jupward - Use a monospace font in the terminal so columns line up
Avoid
j >= ifor the space test — that turns the peak column into a space and drops the widest letter on the row- Mixing different
endCharin outer vs inner loops
Key Takeaways
Descending j over a fixed range yields reverse order plus padding.
j > i prints spaces; j <= i prints letters.
Leading space count is endChar - i.
O(n²) for n rows with width n.
❓ Frequently Asked Questions
i to A, only shifted right.E through A; shorter rows pad on the left inside that same width.j == i and skip printing the row’s widest letter. Stick to j > i for this shape.Explore More C Alphabet Patterns!
Right alignment is “print the padding, then the payload” inside a fixed column count.
This is the same geometry as printing a left-justified reverse triangle, then shifting every line right by endChar - i spaces.
12 people found this page helpful
