Shape Rule
Odd widths, centered
Rows print 1, 3, 5… letters under a fixed bottom width.

Print a centered pyramid: one letter on the first row, then 3 letters, then 5 letters, with leading spaces so it looks aligned. Letters flow continuously via one counter: A, then B C D, then E F G H I. Compare Program 14 (odd widths, no centering) and Program 13 (sequential, left-aligned). Includes a live preview, worked Java examples, edge cases, and complexity.
Odd widths, centered
Rows print 1, 3, 5… letters under a fixed bottom width.
Step by 2
for (int i = 1; i <= width; i += 2) picks each odd row width.
Pad then letters
When j > i print a space; otherwise print the next letter.
Never reset
One k (or index) walks A, B, C… across the whole pyramid.
1–5 rows
Pick a pyramid height and draw it instantly in the browser.
Complexity
Each row scans O(width) columns; overall work is O(r²).
A centered alphabet pyramid grows by two letters on each new line and pads the left with spaces so shorter rows sit under the widest row. Letters stay consecutive across the whole shape — they do not restart at A each row.
In Java you usually solve it with nested loops: the outer loop steps odd widths, the inner loop scans a fixed bottom width printing spaces or the next letter, then System.out.println() ends the row.
It combines three beginner skills at once: odd-width growth, leading-space centering, and a continuous letter counter — the same toolkit used for many pyramids and diamonds.
Rows print 1, 3, 5, … letters.
Pad left so short rows stay centered.
One counter never resets between rows.
Inner loop always walks the bottom width.
In short: for each odd width i, scan the bottom width: print spaces while outside i, otherwise print the next letter, then call println().
Given an odd bottom width (like 5) or a row count, print a centered pyramid of consecutive alphabet letters with leading spaces.
// Three rows (conceptual shape; spaces matter)
// A
// B C D
// E F G H I | Item | Type | Description |
|---|---|---|
width / rows | int | Odd bottom width (1, 3, 5, …) or number of pyramid rows. Width = 2*rows - 1. |
| Printed output | text | Centered odd-width rows of consecutive letters with leading spaces. |
k = 'A' (or index 0 into A..Z)
for i in 1, 3, 5, ... width:
for j from width down to 1:
if j > i: print space
else: print next letter (+ optional trailing space)
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed-width scan | Inner loop always walks width columns | Matching this classic sample |
| Explicit pad + letters | Print (width-i) spaces, then i letters | Clearer reading / teaching rewrite |
| Goal | Pattern |
|---|---|
| Odd row widths | for (int i = 1; i <= width; i += 2) |
| Scan columns | for (int j = width; j >= 1; j--) |
| Leading pad | if (j > i) System.out.print(" "); |
| Next letter | System.out.print(k + " "); k++; |
| End the row | System.out.println(); |
| No centering | See Program 14 |
Same pyramid — different roles on each inner-loop pass.
padPrinted while j > i to center the row
fillPrinted when inside the current odd width
nextAdvances the continuous alphabet stream
breakEnds the row after the full column scan
Reach for this pyramid when teaching centering and continuous fills together.
Keep odd widths; add leading spaces for a centered look.
Practice left padding the same way star pyramids do.
Reuse the running-letter idea from Program 13 with centering.
Once centering clicks, inverted and full diamond shapes follow.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in odd-width growth, padding, and continuous fill at the same time.
Choose a pyramid height between 1 and 5 rows (bottom width = 2×rows−1) and draw it in the browser.
Three complete Java programs — fixed width 5, odd-width input, and an explicit pad-then-letters rewrite. Click View Output to reveal sample console results.
Print a three-row pyramid with a fixed-width scan.
5Hard-coded bounds — ideal for first demos and screenshots.
public class CenteredPyramid {
public static void main(String[] args) {
int k = 0;
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (int i = 1; i <= 5; i += 2) {
for (int j = 5; j >= 1; j--) {
if (j > i) {
System.out.print(" ");
} else {
System.out.print(alpha[k++] + " ");
}
}
System.out.println();
}
}
} When i = 1, four columns print spaces and one prints A. When i = 3, two spaces then B C D. When i = 5, the full width prints E F G H I.
Let the user choose an odd bottom width.
Read the bottom width as an odd number (like 5 or 7). Check hasNextInt() and odd validation in real apps.
import java.util.Scanner;
public class CenteredPyramidInput {
public static void main(String[] args) {
int width;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the bottom width (odd number): ");
width = sc.nextInt();
char k = 'A';
for (int i = 1; i <= width; i += 2) {
for (int j = width; j >= 1; j--) {
if (j > i) {
System.out.print(" ");
} else {
System.out.print(k + " ");
k++;
}
}
System.out.println();
}
sc.close();
}
} Same centering scan as Example 1; only the outer/inner bounds follow width. Require an odd width so rows stay 1, 3, 5, … under a matching bottom line.
Same pyramid with separate pad and letter loops.
Often clearer to read: print leading spaces first, then the odd letter count.
public class CenteredPyramidExplicit {
public static void main(String[] args) {
int rows = 3;
int width = 2 * rows - 1;
char k = 'A';
for (int row = 1; row <= rows; row++) {
int letters = 2 * row - 1;
int pad = width - letters;
for (int s = 0; s < pad; s++) {
System.out.print(" ");
}
for (int L = 0; L < letters; L++) {
System.out.print(k);
if (L < letters - 1) System.out.print(" ");
k++;
}
System.out.println();
}
}
} Row r needs 2r-1 letters and width - letters leading spaces. Spaces between letters are separators only on the letter loop — same visual pyramid as the scan version.
Import java.util.Scanner when reading input. Create a running letter counter (index or char).
i runs 1, 3, 5… — how many letters appear on the row.
Walk the bottom width. If j > i, print a space; else print the next letter and advance the counter.
System.out.println() ends the row so the next odd width starts fresh.
r rows scan O(width) columns each — O(r²) time, O(1) extra memory.
5Trace each outer value of i and see how many pads vs letters print.
i | Leading spaces | Letters | 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 |
Total letters: 1 + 3 + 5 = 9 (A through I). Pads: 4 + 2 + 0 = 6.
Where this centered pyramid (and its padding idea) shows up beyond the homework prompt.
Clearest alphabet demo that leading spaces create a pyramid.
Example: remove pads and watch rows snap left.
Same odd widths — with or without centering.
Example: side-by-side left-aligned vs padded.
Keep a running counter across padded rows.
Example: reset k each row and compare to Program 1-style prefixes.
Same pad math works if you print * instead of letters.
Example: swap letter prints for print("* ").
Fixed-width scans make O(r²) easy to count.
Example: 3 rows × 5 columns = 15 inner iterations.
Practice requiring odd widths before drawing.
Example: reject even width and re-prompt.
Pro Tip: say “pad first, then consecutive letters” before coding — that story prevents resetting k or forgetting spaces.
Why this pattern earns a spot after left-aligned odd-width triangles.
Missing pads or a reset counter show up immediately as a broken pyramid.
Same padding logic works for classic * pyramids.
Fixed-width scan or explicit pad/letter loops teach the same shape.
Streaming output needs no storage beyond counters.
Pro Tip: learn the classic scan version first; treat the explicit pad/letter rewrite as a clarity upgrade afterward.
Small habits that keep centered pyramid code clean.
Use 1, 3, 5, … so the pyramid stays symmetric under the bottom row.
Do not reset k each row if you want continuous letters.
hasNextInt()Avoid crashes when the user types letters instead of a number.
Width 9 uses 25 letters (A–Y); larger bottoms may pass Z.
Trace pads 4 / 2 / 0 on paper before coding larger demos.
Pro Tip: if every row starts with A, you almost certainly reset the letter counter inside the outer loop.
Mistakes that commonly break centered alphabet pyramids.
Rows shift left and no longer look like a pyramid.
→ Print pads while j > i (or print width - letters spaces first).
Each row starts at A again — that is a different pattern.
→ Keep k outside the outer loop.
Even widths break the 1, 3, 5… symmetry under the base.
→ Require an odd width (or derive width = 2*rows - 1).
Letters or empty input throw InputMismatchException.
→ Check hasNextInt() and re-prompt on failure.
Large odd widths need more than 26 letters.
→ Cap width so 1+3+…+width ≤ 26, or define wrap/stop policy.
Check these inputs before calling the solution done.
Output is just A on one line.
Three rows through I.
Reject or bump to next odd for a clean pyramid.
25 letters (A–Y) — last full A–Z-friendly odd width.
nextInt() throws — check hasNextInt().
Same loops work with k = 'a'.
Try these variations to lock in the pattern.
* instead of lettersk = 'A' each row oncewidth = 2*rows - 1 is a safe formula.Quick Takeaway: step odd widths, pad on the left, print consecutive letters, then break the line — that is the whole pyramid.
| Program | Time | Extra space |
|---|---|---|
| Fixed-width scan (Examples 1–2) | O(r²) | O(1) |
| Explicit pad + letters (Example 3) | O(r²) | O(1) |
With bottom width w = 2r-1, each of the r rows scans O(w) columns, so total work is O(r²).
The centered alphabet pyramid is a small nested-loop exercise with lasting payoff: odd-width growth, leading-space centering, and a continuous letter stream. Master the classic fixed-width scan, then optionally rewrite it as explicit pad + letter loops.
Practice the three examples above, then continue to Program 17’s reverse alphabet with a diagonal star.
Use an odd bottom width, pad while outside the current width, advance one letter counter across all rows, and break only after the scan.
width = 2*rows - 1)hasNextInt() and odd-width validationprintln inside the letter loopPrint the centered alphabet pyramid the beginner-friendly way.
Odd widths, centered pads
DefinitionWidths 1, 3, 5…
CodeContinuous letters
CodeEnds each scan
I/OO(r²) time
AnalysisThis 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.
Next up: reverse alphabet rows with a moving diagonal star.
12 people found this page helpful