A right-aligned alphabet pyramid prints growing prefixes of the alphabet (A, A B, A B C, …) pushed right with leading spaces so every row shares the same right edge.
Remember
Rule: for row letter i from A to top,
print (top - i) spaces, then A..i with width 2
A
A B
A B C
A B C D
A B C D E ← top = 'E' (monospace)
Letters restart from A every row (like Program 1), but leading spaces shrink so the right edge stays fixed. Compare with Program 26 (fixed-length rotating rows).
Approach
How to Solve It
Two ways to emit the same shape — nested char loops with pad + %2c, or integer row indexes.
Method
Idea
Best for
Char loops + %2c
Pad top - i spaces; print A..i
Learning, interviews, exams
Integer row indexes
Pad n - row spaces; letter = 'A' + L
When you already think in row numbers
Pseudocode
Pseudocode
for i from 'A' to top:
for j from top down to just above i:
print space
for k from 'A' to i:
print k with width 2
print newline
Cheat sheet
Goal
Pattern
Walk each row letter
for (i = 'A'; i <= top; i++)
Leading spaces
for (j = top; j > i; j--) printf(" ");
Print A..i
for (k = 'A'; k <= i; k++) printf("%2c", k);
End the row
printf("\n");
Pad count
top - i spaces (char math)
Integer rewrite
Pad n - row; letter (char)('A' + L)
Printing Letters vs Starting a New Line
API
Effect
Use for
printf(" ")
Stays on the same line
Each leading space
printf("%2c", k)
Letter in a width-2 field
Each letter on the row
printf("\n")
Ends the current line
After pad and letter loops
Try it
Live Preview
Change the letter count (top = A + n - 1) and the right-aligned pyramid updates instantly — capped at 10 for readability.
Whole numbers from 1 to 10. With n = 5, top is E and you get the classic sample. Best viewed in monospace.
Live result5 rows · 15 letters
A
A B
A B C
A B C D
A B C D E
Trace
Worked Walkthrough — top = 'D'
Trace each row letter i with top fixed at 'D'. Pad count is top - i; letters use width 2.
Row i
Pad spaces
Letters
Printed row
'A'
3
A
A
'B'
2
A B
A B
'C'
1
A B C
A B C
'D'
0
A B C D
A B C D
Letter prints: 1 + 2 + 3 + 4 = 10. Space prints add another triangular count — still O(n²) overall.
Code
C Programs
Three complete programs: fixed top letter, scanf input, and an integer-index rewrite. Use View Output to reveal sample results.
Example 1 — Fixed top at 'E'
Hard-coded range — shrink leading spaces, then print A..i with %2c.
C
#include <stdio.h>
int main(void)
{
char i, j, k;
for (i = 'A'; i <= 'E'; i++)
{
for (j = 'E'; j > i; j--)
{
printf(" ");
}
for (k = 'A'; k <= i; k++)
{
printf("%2c", k);
}
printf("\n");
}
return 0;
}
Output
A
A B
A B C
A B C D
A B C D E
How It Works
1. Outer loop picks the row letter.i runs from 'A' to 'E' — one row per end letter.
2. Pad loop shrinks. Print spaces while j counts from 'E' down past i — fewer spaces each row.
3. Letter loop restarts at A.printf("%2c", k) prints A through i in even columns.
4. Break the line.printf("\n") after both inner loops starts the next wider prefix.
Example 2 — User Input Version
Read the top letter at runtime with scanf. Prefer a leading space in the format and validate A–Z.
C
#include <stdio.h>
int main(void)
{
char top, i, j, k;
printf("Enter top letter (like E): ");
scanf(" %c", &top);
for (i = 'A'; i <= top; i++)
{
for (j = top; j > i; j--)
{
printf(" ");
}
for (k = 'A'; k <= i; k++)
{
printf("%2c", k);
}
printf("\n");
}
return 0;
}
Output (when user enters D)
Enter top letter (like E): D
A
A B
A B C
A B C D
How It Works
1. Prompt and read.scanf(" %c", &top) — the leading space skips leftover newlines.
2. Same pad + prefix core. Only the upper bound changes from hard-coded 'E' to top.
3. Safer input tip. Reject lowercase or non-letters:
Safer input
if (scanf(" %c", &top) != 1 || top < 'A' || top > 'Z')
{
printf("Enter an uppercase letter A–Z.\n");
return 1;
}
Example 3 — Integer row indexes
Same shape thinking in row numbers: pad n - row spaces, then print row letters from A.
C
#include <stdio.h>
int main(void)
{
int n = 5;
int row, s, L;
for (row = 1; row <= n; row++)
{
for (s = 0; s < n - row; s++)
{
printf(" ");
}
for (L = 0; L < row; L++)
{
printf("%2c", (char)('A' + L));
}
printf("\n");
}
return 0;
}
Output
A
A B
A B C
A B C D
A B C D E
How It Works
1. Map size to rows.n = 5 means five lines — same as top letter 'E'.
2. Pad with integers.n - row spaces on each line — identical shrinking pad.
3. Letter from index.(char)('A' + L) for L = 0..row-1 rebuilds A..i.
Edge Cases & Pitfalls
Check these before calling the solution done.
No pad
Left-aligned by mistake
Skipping the space loop reprints Program 1’s shape (plus %2c gaps). Keep the pad loop for right alignment.
%c only
Uneven columns
Plain printf("%c", k) still works but looks tighter. Use %2c when you want even letter columns.
\n early
Broken rows
Call printf("\n") only after both the pad and letter loops.
k++
Continuous stream
A single advancing letter across all rows is a different pattern. Restart k at 'A' every row.
top = 'A'
Single A
One row with no leading spaces and a single width-2 A — a good sanity check.
scanf
Leading space in format
Use scanf(" %c", &top) so a leftover newline is not read as the letter.
Analysis
Time and Space Complexity
Program
Time
Extra space
Char loops (Examples 1–2)
O(n²)
O(1)
Integer rows (Example 3)
O(n²)
O(1)
Each of n rows prints up to O(n) spaces and O(n) letters — quadratic in n.
Remember
Key Takeaways
Rule: shrink leading spaces, then print A..i with %2c.
Restart at A: every row begins the letter loop at 'A' again.
Break the row: call printf("\n") only after pad and letter loops.
Complexity:O(n²) time; O(1) extra space.
One line: for each i, print top - i spaces, then A..i with %2c, then printf("\n").
Frequently Asked Questions
The first inner loop prints (top - i) leading spaces, fewer on each lower row, so the letter block ends at the same right edge.
Width-2 formatting prints each letter in an even column so the spaced layout lines up in a monospace font.
Program 1 is left-aligned with no padding and no spaces between letters (A, AB, ABC). This pattern adds leading spaces and uses %2c so columns stay even.
Remove the leading-space loop and print letters directly from A to the row letter.
printf("%2c", k) prints a letter (with padding to width 2) and stays on the same line. printf("\n") ends the current line after the pad and letter loops.
O(n²) for n rows because each row prints O(n) spaces plus O(n) letters.
Use scanf(" %c", &top) with a leading space, require A–Z, and reject non-letters.
Yes. Pad n − row spaces, then print row letters as (char)('A' + L). Example 3 on this page shows that style.
🤔
Did you know?
Leading padding: for each row letter i, print top - i spaces. Then print A through i with %2c so columns look even in monospace output. The last row has no padding; all rows share the same right edge.