Shape Rule
Diamond halves
Top half grows 1..levels; bottom half mirrors levels-1..1.

The centered number diamond prints 1, 123, 12345, … 123456789, then mirrors back down — a natural follow-up after Program 43’s right-aligned triangle. This tutorial covers two outer loops, centering spaces, ascending digit sequences, a live preview, worked C++ examples, edge cases, and complexity.
Diamond halves
Top half grows 1..levels; bottom half mirrors levels-1..1.
i = 1..levels
for (i = 1; i <= levels; i++) — builds the growing half of the diamond.
i = levels-1..1
for (i = levels - 1; i >= 1; i--) — mirrors the top half back down.
Center rows
Leading spaces before digits — j = i..levels-1 on top, j = levels..i+1 on bottom.
Levels 3–5
Pick a height and draw the centered number diamond in the browser.
Complexity
Each row prints 2*i-1 digits — total work scales as n².
A centered number diamond prints ascending digit sequences on each row, growing to a peak then mirroring back down. With levels = 5, you get 1, 123, … 123456789, then the same rows in reverse.
In C++ you use two outer loops (top and bottom halves), a space loop to center each row, and an inner loop that prints digits 1 to 2*i-1.
It combines symmetric diamond logic with centering spaces — a step up from Program 43’s right-aligned triangle.
Inner loop k < i*2.
Leading spaces per row.
Top 1..levels, bottom levels-1..1.
Follow Program 43; continue to Program 45 next.
In short: top loop i = 1..levels, space loop then digits k = 1..2*i-1, bottom loop i = levels-1..1, then cout << "\n".
Given levels = 5, print a centered number diamond: top half i = 1..levels, bottom half i = levels-1..1, each row with leading spaces then digits 1 to 2*i-1.
// levels = 5
// 1
// 123
// 12345
// 1234567
//123456789
// 1234567
// 12345
// 123
// 1 | Item | Type | Description |
|---|---|---|
levels | int | Diamond peak height — total lines = 2*levels - 1. |
i | int | Outer loop — current row level (top or bottom half). |
j | int | Space loop — prints leading spaces to center the row. |
k | int | Number loop — prints digits 1..2*i-1 via k < i*2. |
for i from 1 to levels:
print (levels - i) spaces
for k from 1 to 2*i-1: print k
print newline
for i from levels-1 down to 1:
print (levels - i) spaces
for k from 1 to 2*i-1: print k
print newline | Approach | Idea | Best for |
|---|---|---|
| Fixed levels | 1, 123, 12345, … | Learning and interviews |
| User-input levels | cin >> levels; | Flexible diamond height |
| Compact trace | levels = 3 on paper first | Debugging loop bounds |
| Goal | Pattern |
|---|---|
| Top half | for (i = 1; i <= levels; i++) |
| Bottom half | for (i = levels - 1; i >= 1; i--) |
| Top space loop | for (j = i; j < levels; j++) cout << " "; |
| Bottom space loop | for (j = levels; j > i; j--) cout << " "; |
| Number loop | for (k = 1; k < i * 2; k++) cout << k; |
| Program 43 contrast | Right-aligned triangle — not a centered diamond |
Same centered number diamond — different ways to control height and trace the loops.
i = 1..levelsGrowing rows to the peak
i = levels-1..1Mirror back down
levels - iCenter each row
k = 1..2*i-1Odd-length sequences
Reach for this pattern when teaching symmetric diamonds, centering spaces, and two-phase loop structures.
Natural follow-up after Program 43 — introduces centering spaces and a mirrored bottom half.
Outer/inner bound practice with an immediate visual check.
Combine loops with cin for a flexible row count.
Compare Program 43 (right-aligned triangle) and Program 45 (next in series) 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 a height between 3 and 5 and draw the centered number diamond in the browser.
Three complete C++ programs — fixed levels, user input, and compact trace demo. Click View Output to reveal sample console results.
Print a centered diamond with levels = 5 using space loops and ascending digit sequences.
levels = 5Hard-coded level count — ideal for first demos and screenshots.
#include <iostream>
using namespace std;
int main() {
int i, j, k;
for (i = 1; i <= 5; i++) {
for (j = i; j < 5; j++)
cout << " ";
for (k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
for (i = 4; i >= 1; i--) {
for (j = 5; j > i; j--)
cout << " ";
for (k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
return 0;
} When i = 1, four spaces center a single 1. When i = 3, two spaces precede 12345. The bottom half mirrors from i = 4 down to 1.
Read the level count with cin instead of hard-coding 5.
Read levels with cin >> levels (check cin.fail() in real apps) and reject non-positive values.
#include <iostream>
using namespace std;
int main() {
int levels;
int i, j, k;
cout << "Enter levels: ";
cin >> levels;
if (cin.fail() || levels <= 0) {
cout << "Please enter a positive integer.\n";
return 1;
}
for (i = 1; i <= levels; i++) {
for (j = i; j < levels; j++)
cout << " ";
for (k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
for (i = levels - 1; i >= 1; i--) {
for (j = levels; j > i; j--)
cout << " ";
for (k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
return 0;
} Same space-and-number loop core as Example 1; only levels comes from user input instead of being hard-coded as 5.
Run with levels = 3 to trace every row on paper before scaling up.
levels = 3Same space and number loops with a smaller level count for quick tracing.
#include <iostream>
using namespace std;
int main() {
int levels = 3;
int i, j, k;
for (i = 1; i <= levels; i++) {
for (j = i; j < levels; j++)
cout << " ";
for (k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
for (i = levels - 1; i >= 1; i--) {
for (j = levels; j > i; j--)
cout << " ";
for (k = 1; k < i * 2; k++)
cout << k;
cout << "\n";
}
return 0;
} Only levels changes from 5 to 3 — the space and number loops stay identical. Trace i = 1, 2, 3 on paper to see how row length grows as 2*i-1.
#include <iostream> using namespace std; for cout. Set loop variables i, j, k with levels = 5.
for (i = 1; i <= levels; i++) — growing rows from 1 to the peak.
for (j = i; j < levels; j++) on top — prints leading spaces to center the row.
for (k = 1; k < i * 2; k++) — prints digits 1 to 2*i-1.
for (i = levels - 1; i >= 1; i--) — mirrors the top half with a different space loop.
2*levels-1 total rows — O(n²) time, O(1) extra memory.
levels = 5, row i = 3Trace row 3 on the top half — spaces, digits printed, and full row output.
| Step | Detail | Output so far |
|---|---|---|
| Space loop | j = 3, 4 — two spaces | |
k = 1..5 | Prints 12345 | 12345 |
cout << "\n" | End row 3 | 12345 |
Characters per row = 2*i-1. Space count on top half = levels - i. Bottom half repeats rows 4, 3, 2, 1 in reverse.
Where this tiny pattern (and its loop structure) shows up beyond the homework prompt.
Clearest visual proof that outer and inner bounds interact.
Example: skip the space loop and watch the diamond snap left.
Foundation for inverted, pyramid, diamond, and hollow variants.
Example: continue to Program 45 for the next pattern in the series.
Practice cout vs cout << "\n" without complex math.
Example: put cout << "\n" inside the inner loop by mistake.
Add spaces between digits once the two-loop structure works.
Example: use cout << k << " " between digits for wider spacing.
Triangular totals make O(n²) concrete for beginners.
Example: count printed digits for levels = 5 — top half alone prints 25 digits.
Pair the pattern with cin.fail() 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 i, space count, and digit loop on paper for levels = 3 before coding — watch how row length grows as 2*i-1.
Small habits that keep number-pattern code clean.
Top half 1..n and bottom half n-1..1 — do not repeat the peak row.
cin.fail()Avoid crashes when the user types letters instead of a number.
Only call cout << "\n" after the inner loop finishes the row.
Mark levels - i spaces before each row’s digits before coding.
Trace i = 1..3 on paper before coding the full levels = 5 demo.
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 number diamond patterns.
Each digit lands on its own line — you get a column, not a triangle.
→ Use cout << k; cout << "\n" only after the number loop.
Without leading spaces, the diamond loses its centered shape.
→ Run the space loop before the number loop on every row.
k <= i * 2 adds an extra digit — row length becomes even instead of odd.
→ Keep for (k = 1; k < i * 2; k++) for exactly 2*i-1 digits.
Starting the bottom loop at i = levels prints the widest row twice.
→ Bottom half starts at i = levels - 1, not levels.
Letters or empty input leave levels unset when cin.fail() is not checked.
→ Check cin.fail() and re-prompt on failure.
Check these inputs before calling the solution done.
Output is just 1 — one row, no bottom half needed.
Outer loop never runs — print nothing or show a message.
levels < 1Treat as invalid; re-prompt instead of silent empty output.
Three rows: 1, 123, 1.
cin without checking cin.fail() is unsafe — validate input.
Total lines = 2*levels - 1 — grows quadratically with peak height.
Try these variations to lock in the pattern.
j > ii = 1..levels without the mirrork from 2*i-1 down to 1i = 1..levels, space loop then k = 1..2*i-1. Bottom starts at levels - 1.cout stays on the line; cout << "\n" advances — mix them carefully.levels > 0 for interactive programs; levels = 1 prints a single centered 1.levels - 1 — do not repeat the peak row at i = levels.Quick Takeaway: top loop i = 1..levels, space loop then digits k = 1..2*i-1, bottom i = levels-1..1, then cout << "\n".
| Program | Time | Extra space |
|---|---|---|
| Nested loops (Examples 1–3) | O(n²) | O(1) |
| Smaller demo (Example 3) | O(n²) | O(1) |
The centered number diamond is a compact lesson in symmetric patterns and centering spaces: print leading spaces, print digits 1 to 2*i-1, grow rows in the top half, then mirror back down. Master the fixed-levels version, then try user input and a smaller trace demo.
Practice the three examples above, then continue to Program 45 for the next pattern in the series.
Bottom half must start at levels - 1 — validate levels when reading from the console.
for (i = 1; i <= levels; i++)for (i = levels - 1; i >= 1; i--)cout << k for k = 1..2*i-1cin.fail() before using levelscout << "\n" inside the inner loopi = levels (repeats peak row)k <= i * 2 instead of k < i * 2levels = 1 edge casePrint the pattern the beginner-friendly way.
Digits per row
DefinitionTop + mirror
Code2*i-1 digits
Codei = levels-1
ShapeO(n²) time
AnalysisThis pattern prints a top half (1..levels) and a bottom half (levels-1..1). Each row prints 2*i-1 digits (1 to 2*i-1) with leading spaces to center the diamond.
Move on to the next pattern in the C++ number-pattern series.
12 people found this page helpful