A centered alphabet pyramid prints odd-width letter rows under a shared bottom width, with leading spaces so each line sits in the middle — letters advance continuously across rows.
Remember
Rule: for odd width i = 1, 3, 5, …,
pad spaces, then print the next i letters
A
B C D
E F G H I ← bottom width = 5
One running counter feeds every letter (A, then B C D, then E F G H I). Centering uses the same padding idea as star pyramids.
Approach
How to Solve It
Two equivalent styles — scan a fixed bottom width with a pad/letter branch, or print pad spaces then letters explicitly.
Method
Idea
Best for
Scan + branch
Walk bottom width; j > i → space, else next letter
Learning, interviews, exams
Pad, then letters
pad = width - letters, then print 2*row-1 letters
Clearer demos once the shape clicks
Pseudocode
Pseudocode
k = 'A'
width = 5 // odd bottom width
for i from 1 to width step 2:
for j from width down to 1:
if j > i:
print space
else:
print k and a space
k = next letter
print newline
Cheat sheet
Goal
Pattern
Odd row widths
for (int i = 1; i <= width; i += 2)
Scan bottom width
for (int j = width; j >= 1; j--)
Pad vs letter
if (j > i) cout << " "; else cout << k++ << " ";
End the row
cout << "\n";
Width from rows
width = 2 * rows - 1;
Printing Letters vs Starting a New Line
API
Effect
Use for
cout << " "
One padding space; stays on the line
Centering columns
cout << k << " "
Letter + separator; stays on the line
Each letter cell
cout << "\n"
Ends the current line
After the inner scan
cout << endl also ends the line (and flushes); "\n" is enough for these demos.
Try it
Live Preview
Change the row count (bottom width = 2n - 1) and the pyramid updates instantly — capped at 5 so letters stay in A–Z.
Whole numbers from 1 to 5. With n = 3, bottom width is 5 and you get the classic sample.
Live result3 rows · 9 letters
A
B C D
E F G H I
Trace
Worked Walkthrough — width = 5
Three rows (i = 1, 3, 5) so padding and the running counter are easy to check by hand.
Row width i
Spaces (j > i)
Letters printed
Printed row
1
4
A
A
3
2
B C D
B C D
5
0
E F G H I
E F G H I
The counter never resets — after A comes B, not another A.
Code
C++ Programs
Three complete programs: fixed bottom width, cin input, and an explicit pad-then-letters rewrite. Use View Output to reveal sample results.
Example 1 — Fixed bottom width 5
Hard-coded bounds — scan each column and branch between space and letter.
C++
#include <iostream>
#include <string>
using namespace std;
int main()
{
int k = 0;
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int i, j;
for (i = 1; i <= 5; i += 2)
{
for (j = 5; j >= 1; j--)
{
if (j > i)
cout << " ";
else
cout << alpha[k++] << " ";
}
cout << "\n";
}
return 0;
}
Output
A
B C D
E F G H I
How It Works
1. Outer loop steps odd widths.i runs 1, 3, 5 — how many letters appear on the row.
2. Inner loop scans the bottom.j walks from 5 down to 1 so every row is the same column width.
3. Branch pad vs letter. When j > i, print a space; otherwise print the next alphabet letter and advance k.
4. Break the line.cout << "\n" after the scan starts the next wider row.
Example 2 — User Input Version
Read an odd bottom width with cin. Validate odd positive width in real apps.
C++
#include <iostream>
using namespace std;
int main()
{
int width, i, j;
char k = 'A';
cout << "Enter the bottom width (odd number): ";
cin >> width;
for (i = 1; i <= width; i += 2)
{
for (j = width; j >= 1; j--)
{
if (j > i)
cout << " ";
else
{
cout << k << " ";
k++;
}
}
cout << "\n";
}
return 0;
}
Output (when user enters 5)
Enter the bottom width (odd number): 5
A
B C D
E F G H I
How It Works
1. Prompt and read.cin >> width sets both loop bounds.
2. Same pyramid core. Only width changes; the pad/letter branch and running k stay identical.
3. Prefer odd widths. Even values break the 1, 3, 5… row sequence under a matching bottom line.
4. Safer input tip. Reject bad or even values:
Safer input
if (!(cin >> width) || width < 1 || width % 2 == 0 || width > 9)
{
cout << "Enter an odd width from 1 to 9.\n";
return 1;
}
Example 3 — Pad spaces, then letters
Same pyramid with separate pad and letter loops — often easier to read.
C++
#include <iostream>
using namespace std;
int main()
{
int rows = 3;
int width = 2 * rows - 1;
char k = 'A';
int row, s, L, letters, pad;
for (row = 1; row <= rows; row++)
{
letters = 2 * row - 1;
pad = width - letters;
for (s = 0; s < pad; s++)
cout << " ";
for (L = 0; L < letters; L++)
{
cout << k;
if (L < letters - 1)
cout << " ";
k++;
}
cout << "\n";
}
return 0;
}
Output
A
B C D
E F G H I
How It Works
1. Derive width from rows.width = 2 * rows - 1 matches the classic odd bottom line.
2. Pad first. Row r needs width - (2r - 1) leading spaces.
3. Then print letters. Print 2r - 1 letters with spaces only between them — same visual pyramid as Examples 1–2.
Edge Cases & Pitfalls
Check these before calling the solution done.
Even width
Broken odd sequence
Require an odd bottom width so rows stay 1, 3, 5… under a matching last line.
Reset k
Letters restart at A
Advance the counter across rows. Resetting each row rebuilds a different pattern.
\n early
Broken rows
Call cout << "\n" only after the inner scan finishes.
width = 1
Single A
One row with a single A — no padding columns.
Past Z
Too many letters
Total letters equal rows². Cap rows (or width) so you stay in A–Z for alphabet demos.
cin
Check failure
Use if (!(cin >> width)) so bad input does not leave width uninitialized.
Analysis
Time and Space Complexity
Program
Time
Extra space
Scan style (Examples 1–2)
O(r²)
O(1)
Pad style (Example 3)
O(r²)
O(1)
For r rows the letter count is 1 + 3 + … + (2r - 1) = r², and each row also walks O(width) pad columns — quadratic in r.
Remember
Key Takeaways
Odd widths: step i by 2 so rows are 1, 3, 5… letters.
Center with pads: print spaces while j > i, then the next letters.
Keep one counter: letters continue across rows — do not restart at A.
Complexity:O(r²) time; O(1) extra space.
One line: for each odd width i, scan the bottom: spaces while outside i, else the next letter, then cout << "\n".
Frequently Asked Questions
The inner loop scans a fixed bottom width. While the column index is still outside the current row width, it prints spaces; otherwise it prints the next letter.
The outer loop increases by 2 (1, 3, 5), so each row prints an odd number of letters, forming a pyramid shape.
Increase the maximum odd width (and loop bounds). The number of rows grows as width grows: 1, 3, 5, 7…
cout << ch stays on the same line. cout << "\n" ends the current line. Spaces and letters use cout; the row break uses cout << "\n" after the inner scan.
The trailing space makes columns easier to see. If you want compact output, print just k (or control separators carefully).
O(r²) for r rows because each row scans a fixed-width set of columns and total letters equal 1+3+…+(2r−1) = r².
After cin >> width, check failure and require an odd positive width (1, 3, 5, …). Cap so total letters stay within A–Z if you want only alphabetic output.
Yes. Printing leading spaces first, then 2*row−1 letters, matches the scan version visually and is often easier to read.
🤔
Did you know?
This pattern combines two ideas: odd-width rows (i += 2) and centering via padding spaces (like star pyramids). Letters flow continuously via one counter — A, then B C D, then E F G H I — while leading spaces keep each row aligned under the widest line.