Right-Aligned Sequential Pyramid in C++

What You’ll Learn
This pattern prints letters in a running sequence (A, then B C, then D E F…) but keeps the triangle right-aligned by printing empty cells first.
For best alignment, view output in a monospace terminal because we rely on fixed-width cells.
⭐ Pattern Output
Output for 5 rows:
A
B C
D E F
G H I J
K L M N OC++ Program (Fixed 5 Rows)
Direct adaptation of the reference logic using ASCII values and setw(2) for aligned columns.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int i, j;
int k = 65;
for (i = 65; i <= 69; i++) {
for (j = 69; j >= 65; j--) {
if (j > i) cout << " ";
else cout << setw(2) << char(k++);
}
cout << "\n";
}
return 0;
}🧠 How It Works
k streams the alphabet
k starts at 65 (A) and only advances when a letter is actually printed via cout << setw(2) << char(k++), so the triangle reads as one continuous sequence laid into a fixed grid.
Outer i sets how many letters appear
i walks row codes 65…69. Row i exposes i - 64 letter slots; the remaining columns on the line stay padded.
Inner j scans columns high → low
For each j from 69 down to 65, if j > i print two spaces; else print the next streamed letter in a width-two field. <iomanip> setw(2) keeps columns aligned on small screens when the line scrolls horizontally.
Why the inner loop is always five wide
You always iterate across the full code range for the pattern, so each row has five logical columns. Padding plus setw(2) preserves the staircase shape regardless of viewport width.
Letter count
1+2+3+4+5 = 15 letters through O. Padding adds more writes; time still scales as O(n²) for n rows in this fixed-grid style.
Variation — User Input
Choose the number of rows and compute the last character for both loops.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
const int base = 65;
int last = base + rows - 1;
int k = base;
for (int i = base; i <= last; ++i) {
for (int j = last; j >= base; --j) {
if (j > i) cout << " ";
else cout << setw(2) << char(k++);
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Switch to
charloops ('A'..) - Add a space after each printed letter for readability
- Validate input so
rowsstays in 1..26
Avoid
- Changing padding width without updating
setw()(misalignment) - Resetting
keach row (sequence breaks)
Key Takeaways
Use one running counter (k++) to print sequential letters.
Keep padding width equal to the letter cell width (setw(2)).
Total letters for n rows is \(n(n+1)/2\).
Overall complexity is O(n²).
❓ Frequently Asked Questions
cout, padding with spaces so the output occupies at least 2 characters.cout << ' ' << char(k++)—but then padding must also be one cell wide to keep alignment consistent.Z (ASCII) unless you limit rows to 26 or change the character set.Explore More C++ Alphabet Patterns!
Right alignment is easiest when you treat each printed item as a fixed-width cell.
The total number of printed letters after n rows is the nth triangular number: \(1 + 2 + \dots + n = n(n+1)/2\).
12 people found this page helpful
