Centered Alphabet Pyramid in C++

What You’ll Learn
Three rows, odd-width letter groups, padded with spaces so the block lines up like a pyramid. Letters flow in order through one counter k.
Related: program 14 (odd-width triangle without centering) and star pyramids (spaces + symbols).
⭐ Pattern Output
Three rows (spaces between letters; leading spaces align columns):
A
B C D
E F G H IComplete C++ Program (Three Rows)
Bottom row ends at 'E' (69); outer i runs 65, 67, 69. Inner j runs 69 down to 65.
#include <iostream>
using namespace std;
int main() {
int i, j;
int k = 65;
for (i = 65; i <= 69; i += 2) {
for (j = 69; j >= 65; j--) {
if (j > i)
cout << \" \";
else
cout << char(k++) << \" \";
}
cout << \"\\n\";
}
return 0;
}🧠 How It Works
k = 65
Continuous alphabet index; not reset each row.
Outer i += 2
Row “cap” letters: A, C, E for this sample.
Inner scan j = 69 .. 65
If j > i, print padding spaces; otherwise print the next alphabet character from k.
First row detail
For i = 65, values j = 69, 68, 67, 66 satisfy j > i (four spaces); j = 65 prints A.
Work per row
This sample always runs five inner steps; for general rows, the inner width is 2 * rows - 1 positions per line, so total work is O(r²).
Variation — User Input
Last outer i is base + 2*(rows-1); inner j starts at the same value. Same j > i rule.
#include <iostream>
using namespace std;
int main() {
int rows, i, j;
int k = 65;
const int base = 65;
cout << \"Enter the number of rows: \";
cin >> rows;
int last = base + 2 * (rows - 1);
for (i = base; i <= last; i += 2) {
for (j = last; j >= base; --j) {
if (j > i) cout << \" \";
else cout << char(k++) << \" \";
}
cout << \"\\n\";
}
return 0;
}💡 Tips for Enhancement
Try These
- Rewrite with character literals:
for (i = 'A'; i <= 'E'; i += 2)and matchingjbounds - Two-loop style: print
(last - i) / 2spaces, then a dedicated letter loop (same geometry, clearer intent) - Clamp
rowssokdoes not pass'Z'
Avoid
- Confusing
j > iwith “odd-only j” — every integer j between bounds is visited - Resetting
keach row (you would repeatAevery line)
Key Takeaways
Fixed-width inner scan + j > i produces leading padding.
k++ only on printed letters keeps the alphabet continuous.
Outer step 2 gives 1, 3, 5, … letters per row.
O(r²) for r rows with width scaling with r.
❓ Frequently Asked Questions
i = 65, values j = 69, 68, 67, 66 satisfy j > i, so you print four spaces; j = 65 prints the letter.base + 2*(rows-1) is the ASCII code of the last row’s end letter. When rows is 3 that is 69 ('E').char(k++) without the trailing \" \". Centering logic is unchanged; only the letter spacing tightens.Explore More C++ Alphabet Patterns!
Centering is often “print padding, then print the payload” — here both share one inner loop.
When tracing row 1, list every j from 69 down to 66 — each is > 65, so you get four padding spaces.
12 people found this page helpful
