Shape Rule
Growing prefixes
A, A B, A B C, … A B C D E.

Right-align the pyramid by printing a shrinking number of leading spaces, then printing letters from A up to the current row letter (restarting each row). Because we print letters using %2c, use a monospace font if you want the right edge to look perfect. Compare Program 22 (right-aligned sequential stream) and Program 16 (centered). Includes a live preview, worked C examples, edge cases, and complexity.
Growing prefixes
A, A B, A B C, … A B C D E.
Shrink spaces
Print top - i spaces so rows share a right edge.
Each row
Letters always run A..i — not a k++ stream.
%2c
Fixed-width letter cells for even columns.
Top letter
Pick a top letter (A–F) and draw the pyramid.
Complexity
Pads + letters per row scale with n.
A right-aligned alphabet pyramid prints growing prefixes of the alphabet (A, A B, A B C, …) pushed to the right with leading spaces so every row shares the same right edge.
In C you solve it with nested char loops: shrink the pad count, then print A..i with optional width formatting.
It combines padding math with per-row letter prefixes — the classic right-aligned triangle before sequential streams or centering.
Shrink leading spaces per row.
Restart letters every row.
All rows share the same end.
Not Program 22’s continuous k++.
In short: for each row letter i, print spaces while j > i, then print A..i with width 2, then call printf("\n").
Given a top letter (or fixed E), print a right-aligned pyramid of alphabet prefixes ending at that letter.
// Five rows (monospace; leading spaces + width-2 letters)
// A
// A B
// A B C
// A B C D
// A B C D E | Item | Type | Description |
|---|---|---|
top | char | Last row letter (e.g. E). Row count = top - 'A' + 1. |
| Printed output | text | Right-aligned prefixes A..i with leading spaces. |
for i from 'A' to top:
for j from top down while j > i:
print one space
for k from 'A' to i:
print k with width 2
print newline | Approach | Idea | Best for |
|---|---|---|
| Char loops (classic) | Pad top..i+1; letters A..i | Matching this sample |
| Int row index | Pad n-row; letters by index | When you prefer int counters |
| Goal | Pattern |
|---|---|
| Outer rows | for (char i = 'A'; i <= top; i++) |
| Leading pads | for (char j = top; j > i; j--) printf(" "); |
| Letters | for (char k = 'A'; k <= i; k++) printf("%2c", k); |
| End row | printf("\n"); |
| Sequential stream | See Program 22 |
Same row — three roles that build the right-aligned pyramid.
padLeading spaces that shrink each row
A..iPrefix letters restarting from A
growEach row adds one more letter on the right
breakEnds the row after pads + letters
Reach for this when teaching leading-space alignment with per-row alphabet prefixes.
First right-aligned alphabet pyramid many courses assign.
Same right edge idea; prefixes vs continuous stream.
Step up to Program 16 after pads feel natural.
Practice %2c letter cells in monospace output.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: shrinking leading spaces while restarting A..i is the clearest way to teach right-aligned alphabet prefixes.
Choose a top letter from A to F and draw the right-aligned alphabet pyramid in the browser (monospace).
Three complete C programs — fixed A–E, scanf top letter, and an int-index style. Click View Output to reveal sample console results.
Print five right-aligned prefix rows from A through E.
First print leading spaces, then print letters A..i using printf("%2c", k).
#include <stdio.h>
int main() {
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;
} When i = 'C', two leading spaces print, then letters A B C via printf("%2c", k). The next row pads once and prints through D, keeping the right edge fixed.
Let the user choose the last letter (like E).
The pattern prints up to that row. Check scanf’s return value and require A–Z in real apps.
#include <stdio.h>
int main() {
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;
} Same pad + prefix rules; only the shared top letter changes. Pad count is always top - i spaces.
Same shape with integer row and column indexes.
Often clearer if you think in row numbers: pad n - row spaces, then print row letters from A.
#include <stdio.h>
int main() {
char top = 'E';
int n = top - 'A' + 1;
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;
} Row 1 prints one letter; row 5 prints five. Pad count is n - row; letter L is (char)('A' + L).
Outer i runs from A to E (or your chosen top).
Loop j = E..(i+1) prints one space per step, making the pyramid right-aligned.
Loop k = A..i prints each letter in a 2-character field using %2c.
printf("\n") ends the row so the next lower pad count can grow the prefix.
Each row does O(n) work for padding plus letters, so total is O(n²).
Trace each row’s pad count, letter prefix, and printed line.
i | Pad spaces | Letters | Printed row |
|---|---|---|---|
A | 4 | A | ····A |
B | 3 | A B | ···A B |
C | 2 | A B C | ··A B C |
D | 1 | A B C D | ·A B C D |
E | 0 | A B C D E | A B C D E |
Pad count = top - i. Letters always restart at A.
Where this right-aligned prefix pyramid shows up beyond the homework prompt.
Clearest demo of shrinking leading spaces for right alignment.
Example: remove pads once and see a left-aligned triangle.
Same right edge — prefixes vs continuous letter stream.
Example: print both for top = E side by side.
Outer and inner loops over ascending char ranges.
Example: rewrite with int indexes (Example 3).
Use %2c so letter columns stay even.
Example: try plain printf("%c", k) and compare spacing.
After right-align, add more pads for a centered look.
Example: see Program 16.
Next pattern builds a symmetric decreasing alphabet square.
Example: continue to Program 28.
Pro Tip: say “fewer spaces, then A through the row letter” before coding — that story prevents a continuous k++ stream by mistake.
Why this pattern earns a spot early in the alphabet-pattern series.
Wrong pad counts or continuous streams show up immediately.
Char loops or int indexes teach the same shape.
A natural place to learn leading-space alignment.
Streaming output needs no storage beyond loop variables.
Pro Tip: learn the classic char-loop version first; treat the int-index rewrite as a clarity option afterward.
Small habits that keep right-aligned prefix pyramids clean.
Always print A..i — do not keep a running k++ for this pattern.
Pad count is top - i; last row has zero pads.
Require a single A–Z character; normalize case if needed.
Proportional fonts make %2c columns look uneven.
Mixing one-space and two-space pads breaks the right edge.
Pro Tip: if rows look like A, B C, D E F, you wrote Program 22’s stream instead of restarting at A.
Mistakes that commonly break right-aligned alphabet prefix pyramids.
Rows become A, B C, D E F… instead of A, A B, A B C.
→ Restart letters from A on every row.
Using j >= i or the wrong bound leaves uneven right edges.
→ Pad while j > i from top downward.
Columns look uneven even when the code is correct.
→ View output in a monospace terminal/font.
scanfEmpty or non-letter input leaves top invalid.
→ Check scanf’s return value and require A–Z.
Switching between one- and two-space pads breaks the right edge.
→ Keep pad characters consistent for the whole program.
Check these inputs before calling the solution done.
Output is just A (no pads).
Five rows through A B C D E.
Three rows (Example 2).
Normalize with char.toupper if needed.
Unchecked scanf fails silently — check the return value.
Skip the pad loop for a left triangle.
Try these variations to lock in the pattern.
k++ instead of A..iA and grows through the row letter.%2c keeps letter columns even in monospace terminals.Quick Takeaway: print shrinking leading spaces, then letters A..i with width 2, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Char pad + letters (Examples 1–2) | O(n²) | O(1) |
| Int row index (Example 3) | O(n²) | O(1) |
Each of n rows does O(n) pad + letter work, so total work is O(n²).
The right-aligned alphabet pyramid is a small nested-loop exercise with lasting payoff: shrinking leading spaces and per-row prefixes from A. Master the classic A…E sample, then try user input and the int-index rewrite.
Practice the three examples above, then continue to Program 28’s symmetric decreasing alphabet square.
Pad while j > i, print A..i with width 2, keep pads consistent, then break the line.
A on every row%2c output in a monospace fontscanf and require an A–Z top letterk++ stream for this patternprintf("\n") inside the pad or letter loopPrint the right-aligned alphabet pyramid the beginner-friendly way.
Pad + A..i
DefinitionRestart each row
Code%2c cells
CodeEnds each row
I/OO(n²) time
AnalysisLeading padding: for each row letter i, the loop prints E - i spaces. Then the letter loop prints A through i using %2c so columns look even in monospace output. The last row has no padding; all rows share the same right edge.
Next up: symmetric alphabet square patterns.
12 people found this page helpful