Right-Aligned Reverse Pyramid in C++

What You’ll Learn
Each row is a reverse suffix A, BA, CBA, … padded on the left so letters line up on the right in a fixed-width column.
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 (Reference Logic)
Outer i is the row peak; inner j sweeps the full alphabet slice once per row from E down to A.
#include <iostream>
using namespace std;
int main() {
int i, j;
for (i = 65; i <= 69; i++) {
for (j = 69; j >= 65; j--) {
if (j > i) cout << " ";
else cout << char(j);
}
cout << "\n";
}
return 0;
}🧠 How It Works
Outer i
How far the reverse run extends this row: down from i to A.
Inner j scan
j always runs from endChar down to startChar, so each line has a fixed width.
if (j > i)
Print a space for letters that belong to higher rows only.
Else branch
Print j, creating a contiguous reverse suffix like CBA.
Leading spaces
Row peak i has endChar - i spaces before the first printed letter.
Variation — User Input
endChar replaces 'E' in the inner loop bound.
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j;
char startChar, endChar;
cout << "Enter the number of rows: ";
cin >> rows;
startChar = 'A';
endChar = (char)('A' + rows - 1);
for (i = startChar; i <= endChar; ++i) {
for (j = endChar; j >= startChar; --j) {
if (j > i) cout << " ";
else cout << (char) j;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Separate loop: print
endChar - ispaces, then printjfromidown tostartChar - Flip to a forward triangle by scanning
jupward - Use a monospace font 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.endChar through A; shorter rows pad on the left inside that same width.j == i and skip printing the widest letter. Stick to j > i.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
