Shape Rule
Centered pyramid
Row 1 prints 1, row 2 prints 2 3 4, row 3 prints 5 6 7 8 9 with leading spaces.

The centered continuous number pyramid prints 1, then 2 3 4, then 5 6 7 8 9 — a natural step after the mirror pattern in Program 23. This tutorial covers odd row widths, leading spaces, a running counter k, a live preview, algorithm steps, worked C++ examples, edge cases, and complexity.
Centered pyramid
Row 1 prints 1, row 2 prints 2 3 4, row 3 prints 5 6 7 8 9 with leading spaces.
i += 2
for (i = 1; i <= max; i += 2) sets odd row widths 1, 3, 5.
if (j > i)
Reverse loop prints spaces first, then k++ for each number slot.
k never resets
k = 1 before the outer loop; k++ continues across rows.
Odd widths 1–9
Pick a max odd width and draw the centered pyramid instantly in the browser.
Complexity
Each row scans max columns; total work scales as n².
A centered continuous number pyramid prints numbers that keep counting across rows, with leading spaces to center each row. With max width 5, the output is 1, 2 3 4, 5 6 7 8 9 (spaces shown in the worked examples below).
In C++ you use an outer loop with odd widths, a reverse inner loop with an if for spaces vs k++, then cout << "\n" ends each row.
It combines spacing logic with a persistent counter — a step up from Program 23’s three inner loops.
i = 1, 3, 5 controls how many numbers print per row.
if (j > i) prints spaces before numbers.
k++ never resets — numbers flow across rows.
Follow Program 23; continue to Program 25 (bidirectional triangle) next.
In short: for each odd i, scan j from max down to 1 — print a space when j > i, else print k++, then cout << "\n".
Given a positive odd max width (e.g. 5), print a centered pyramid where numbers increase continuously across rows using a counter k.
// max = 5 (conceptual shape — dots show spaces)
// ··1·
// ·2·3·4
// 5·6·7·8·9 | Item | Type | Description |
|---|---|---|
max | int | Maximum odd row width — inner loop scans j from max down to 1. |
i | int | Outer loop — odd row widths 1, 3, 5 via i += 2. |
j | int | Reverse inner loop — spaces when j > i, else print number. |
k | int | Running counter — starts at 1, increments with k++ across all rows. |
k = 1
for i from 1 to max step 2:
for j from max down to 1:
if j > i:
print space
else:
print k; k = k + 1
print newline | Approach | Idea | Best for |
|---|---|---|
| Spacing + counter | 1, 2 3 4, 5 6 7 8 9 | Learning and interviews |
| User-input max | cin >> max; | Flexible console programs |
| Safe input | cin loop + even-width adjustment | Robust user-facing demos |
| Goal | Pattern |
|---|---|
| Walk rows | for (i = 1; i <= max; i += 2) |
| Init counter | k = 1; before the outer loop |
| Scan columns | for (j = max; j >= 1; j--) |
| Space or number | if (j > i) cout << " "; else cout << k++ << " "; |
| End the row | cout << "\n"; |
| User input | cin >> max; |
Same centered pyramid — different ways to control width and input validation.
i += 2Odd row widths 1, 3, 5
j > iLeading spaces center each row
k++Numbers continue across rows
if/elseOne inner loop handles space vs number
Reach for this pattern when teaching centering with spaces, persistent counters, and if/else inside nested loops.
Natural follow-up after Program 23 — introduces spacing logic and a running counter.
Outer/inner bound practice with an immediate visual check.
Combine loops with cin for a flexible row count.
Compare Program 23 (mirror pattern) and Program 25 (bidirectional triangle) next.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one small program that locks in nested loops, output sequencing, and O(n²) thinking.
Choose an odd max width between 1 and 9 and draw the centered continuous pyramid in the browser.
Three complete C++ programs — fixed max width, user input, and safe input with validation. Click View Output to reveal sample console results.
Print three rows of the centered pyramid with a running counter.
max = 5Hard-coded width — ideal for first demos and screenshots.
#include <iostream>
using namespace std;
int main() {
int i, j, k;
k = 1;
for (i = 1; i <= 5; i += 2) {
for (j = 5; j >= 1; --j) {
if (j > i)
cout << " ";
else
cout << k++ << " ";
}
cout << "\n";
}
return 0;
} When i = 1, print two spaces then 1 — output 1. When i = 3, print one space then 2 3 4. When i = 5, print 5 6 7 8 9 with no leading spaces. k never resets, so numbers continue across rows.
Read the maximum odd width with cin instead of hard-coding 5.
Read max with cin >> max (check cin.fail() in real apps); adjust even widths to the nearest odd value.
#include <iostream>
using namespace std;
int main() {
int max;
int i, j, k;
cout << "Enter the maximum odd width: ";
cin >> max;
if (max % 2 == 0) max -= 1;
if (max < 1) return 0;
k = 1;
for (i = 1; i <= max; i += 2) {
for (j = max; j >= 1; --j) {
if (j > i)
cout << " ";
else
cout << k++ << " ";
}
cout << "\n";
}
return 0;
} Same spacing + counter core as Example 1; only the source of max changes. The even-width adjustment keeps row sizes odd for a proper pyramid shape. Non-numeric input sets cin’s fail bit if you ignore errors — always validate in safer labs.
Check cin.fail() so bad input does not leave max unset.
cin LoopValidate input before drawing the pyramid — prompt again on failure.
#include <iostream>
using namespace std;
int main() {
int max;
int i, j, k;
cout << "Enter the maximum odd width: ";
while (!(cin >> max) || max < 1) {
cout << "Please enter a positive whole number: ";
}
if (max % 2 == 0) max -= 1;
k = 1;
for (i = 1; i <= max; i += 2) {
for (j = max; j >= 1; --j) {
if (j > i)
cout << " ";
else
cout << k++ << " ";
}
cout << "\n";
}
return 0;
} cin.fail() is true on bad input — the loop re-prompts until a valid positive integer is entered, then the pyramid draws as usual.
#include <iostream> brings in cout and cin. Set k = 1 and loop variables i, j.
for (i = 1; i <= max; i += 2) — row widths 1, 3, 5 grow the pyramid.
for (j = max; j >= 1; j--) scans columns from right to left.
if (j > i) prints a space; else cout << k++ << " ".
cout << "\n" ends the row after the inner loop.
Numbers continue across rows — O(n²) time, O(1) extra memory.
max = 5Trace each outer-loop value of i, leading spaces, numbers printed, and k after each row.
i | Leading spaces | Numbers printed | k after row | Row output |
|---|---|---|---|---|
1 | 2 (when j = 5, 4) | 1 | 2 | 1 |
3 | 1 (when j = 5) | 2, 3, 4 | 5 | 2 3 4 |
5 | 0 | 5, 6, 7, 8, 9 | 10 | 5 6 7 8 9 |
Leading spaces per row = (max - i) / 2 when max is odd — centers each row.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: put cout << "\n" inside the inner loop by mistake.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: reset k each row and compare output.
Practice cout vs row newline without complex math.
Example: put cout << "\n" inside the inner loop by mistake.
Swap digits for letters, stars, or spaced output once the loop works.
Example: print k++ + " " with padded widths for 2-digit numbers.
Triangular totals make O(n²) concrete for beginners.
Example: count printed numbers for max = 9 → 1 + 3 + 5 + 7 + 9 = 25.
Pair the pattern with cin.fail() return checks and positive-row checks.
Example: reject max <= 0 and re-prompt.
Pro Tip: when an interviewer asks for patterns, explain the outer/inner roles first — then write the loops. The story matters as much as the code.
Why this pattern earns a permanent spot in beginner C++ courses.
Wrong bounds show up immediately as a broken staircase.
Only loops and console output — no arrays or math libraries.
Invert, center, hollow, or change the fill character with small edits.
Streaming output needs no storage beyond loop counters.
Pro Tip: trace the reverse inner loop on paper for max = 3 before coding — spacing bugs hide in the j > i condition.
Small habits that keep number-pattern code clean.
Do not reset k inside the outer loop unless you want per-row numbering.
cinCheck cin.fail() so bad input does not leave max unset.
Only call cout << "\n" after the inner loop finishes the row.
Write each i, space count, and numbers printed before coding.
Trace max = 3 on paper before coding larger demos.
Pro Tip: if the output is a vertical list of single digits per line, you almost certainly put cout << "\n" inside the inner loop.
Mistakes that commonly break centered pyramid patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use cout << " " or cout << k++ << " "; cout << "\n" only after the inner loop.
Putting k = 1 inside the outer loop restarts numbering — you lose the continuous effect.
→ Initialize k = 1 once before the outer loop unless you want per-row numbering.
Without if (j > i) the pyramid is left-aligned, not centered.
→ Print a space when j > i before printing numbers.
Even max values break the centering math for this version.
→ Subtract 1 when max % 2 == 0, or validate and prompt again.
Letters or empty input leave max unset.
→ Check cin.fail() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just a centered 1 with leading spaces.
Outer loop never runs — print nothing or show a message.
max < 0Treat as invalid; re-prompt instead of silent empty output.
Subtract 1 to force odd width, or re-prompt for an odd value.
Unchecked cin leaves max unset — check cin.fail().
Two rows: centered 1 and 2 3.
Try these variations to lock in the pattern.
k = 1 inside the outer loopk++ on charsj > i shift numbers right — row width stays at max columns.cout stays on the line; cout << "\n" advances — mix them carefully.max > 0 for interactive programs; max = 1 prints a single centered 1.Quick Takeaway: odd outer loop (i += 2), reverse inner loop with if (j > i), persistent k++, then cout << "\n" after each row.
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–2) | O(n²) | O(1) |
| Safe input (Example 3) | O(n²) | O(1) |
The centered continuous number pyramid is a compact lesson in spacing and counters: print leading spaces when j > i, then k++ for each number slot. Master the fixed-max version, then try user input and safe cin validation.
Practice the three examples above, then continue to Program 25 for the bidirectional number triangle.
Never reset k inside the outer loop unless you want per-row numbering — validate max when reading from the console.
for (i = 1; i <= max; i += 2) in the outer loopk = 1 before the outer loopj > i, else k++cin.fail() before using maxcout << "\n" inside the inner loopk inside the outer loop (unless intentional)max without adjustmentmax = 1 edge casePrint the pattern the beginner-friendly way.
Spaces + k++
DefinitionOdd widths
CodeCentering
CodeNever reset
ShapeO(n²) time
AnalysisThis centered pyramid prints numbers continuously using a counter k. An if inside a reverse loop prints leading spaces when j > i, then prints k++ once the column reaches the row boundary.
Move on to the bidirectional number triangle in the C++ number-pattern series.
12 people found this page helpful