C++ Number Diamond Pattern

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A centered number diamond prints ascending digit sequences on each row — growing to a peak of odd length, then mirroring back down — with leading spaces to keep the shape centered.

Remember
Rule: row i prints 1..(2*i-1); top then bottom half

    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1         ← levels = 5

Unlike Program 43 (right-aligned triangle only), this shape has two phases: grow to the peak, then shrink symmetrically.

How to Solve It

Run a top half that grows, then a bottom half that shrinks — each row: spaces, then digits 1..(2*i-1).

MethodIdeaBest for
Two outer loopsTop 1..levels, bottom levels-1..1Learning, interviews, exams
Spaces + digitsIndent, then k = 1 .. 2*i-1Centered diamond demos

Pseudocode

Pseudocode
for i from 1 to levels:          // top
    print (levels - i) spaces
    for k from 1 to (2*i - 1):
        print k (no newline)
    print newline

for i from levels-1 down to 1:   // bottom
    print (levels - i) spaces
    for k from 1 to (2*i - 1):
        print k (no newline)
    print newline

Cheat sheet

GoalPattern
Top halffor (i = 1; i <= levels; i++)
Bottom halffor (i = levels - 1; i >= 1; i--)
Top spacesfor (j = i; j < levels; j++) cout << " ";
Bottom spacesfor (j = levels; j > i; j--) cout << " ";
Digits on row ifor (k = 1; k < i * 2; k++) cout << k;
End the rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << " " / cout << kStays on the same lineEach space or digit
cout << "\n"Ends the current lineAfter the digit loop

Print spaces and digits without a newline, then end the row once. cout << endl also ends the line and flushes; "\n" is enough for these demos.

Live Preview

Change the level count and the centered diamond updates instantly — capped at 5 so peak digits stay single-digit (1..9).

Whole numbers from 1 to 5. Tap a chip or type a value — the preview redraws as you go.

Live result 5 levels · 41 digits
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1

Worked Walkthrough — levels = 3

Trace each half: spaces, digit count 2*i-1, and the printed row.

HalfiSpacesDigitsPrinted row
Top1211
Top21123123
Top301234512345
Bottom21123123
Bottom1211

Peak row is printed once in the top half — the bottom starts at levels - 1 so it is not duplicated. Digit total for n levels = n² + (n-1)².

C++ Programs

Three complete programs: fixed levels, cin input, and a compact trace demo. Use View Output to reveal sample results.

Example 1 — Fixed levels = 5

Hard-coded level count — ideal for first demos and screenshots.

C++
#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;
}

How It Works

1. Top half grows. i runs 1..5; spaces shrink while digits grow to 2*i-1.

2. Digit loop. for (k = 1; k < i * 2; k++) prints exactly 2*i-1 digits — e.g. i = 3 prints 12345.

3. Bottom half mirrors. i runs 4..1 so the peak is not printed twice; spaces grow again as rows shrink.

Example 2 — User Input Levels

Read the level count at runtime and use it in both halves. Prefer validating cin (tip below).

C++
#include <iostream>
using namespace std;

int main()
{
    int levels;
    int i, j, k;

    cout << "Enter levels: ";
    cin >> levels;

    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;
}

How It Works

1. Prompt and read. Ask for a level count, then store it in levels.

2. Same diamond core. Only the source of levels changes — top and bottom logic match Example 1.

3. Safer input tip. Prefer:

Safer input
if (!(cin >> levels) || levels < 1)
{
    cout << "Enter a whole number of levels (1 or more).\n";
    return 1;
}

Example 3 — Compact levels = 3

Same space and number loops with a smaller level count for quick tracing on paper.

C++
#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;
}

How It Works

1. Only levels changes. The space and digit loops stay identical to Examples 1 and 2.

2. Trace on paper. Row lengths are 1, 3, 5, then 3, 1 — easy to check by hand before scaling up.

3. Peak once. Bottom starts at levels - 1, so 12345 appears only once.

Edge Cases & Pitfalls

Check these before calling the solution done.

peak twice

Duplicated middle

If the bottom half starts at levels instead of levels - 1, the widest row prints twice. Start at levels - 1.

no spaces

Left-snapped diamond

Without the space loops, rows grow and shrink flush left. Keep the indent loops for centering.

k <= i

Wrong digit count

Stopping at i prints only i digits. Use k < i * 2 for odd lengths 1, 3, 5, ….

\n early

Column of digits

If cout << "\n" is inside the digit loop, each digit lands on its own line. End the row only after the digit loop.

levels > 5

Multi-digit values

For i > 5, k reaches 10+. Cap at 5 for a clean single-digit diamond, or format with spaces/setw.

Bad cin

Validate levels

Check cin >> levels and require levels >= 1 before the loops.

Time and Space Complexity

ProgramTimeExtra space
Top + bottom halves (Examples 1–3)O(levels²)O(1)

Top digits = n²; bottom digits = (n-1)²; total = n² + (n-1)² — still quadratic in n. Only a few integers of extra memory.

Key Takeaways

  • Rule: row i prints 1..(2*i-1); grow with top half, shrink with bottom half.
  • Peak once: start the bottom half at levels - 1 so the widest row is not duplicated.
  • Break the row: call cout << "\n" only after the digit loop.
  • Complexity: O(n²) time from the digit totals; O(1) extra space.

One line: for each half, print spaces then digits 1..(2*i-1), grow to the peak, then mirror down without reprinting it.

Frequently Asked Questions

A centered diamond of ascending digit sequences: 1, 123, 12345, 1234567, 123456789, then mirrors back down to 1.
The row prints 2*i-1 digits (odd length): 1, 3, 5, 7, 9, ... using for (k = 1; k < i*2; k++).
A space loop prints leading spaces before the numbers. Top half uses for (j = i; j < levels; j++); bottom half uses for (j = levels; j > i; j--).
The first loop builds the top half (i = 1..levels). The second mirrors back down (i = levels-1..1) to complete the diamond.
Program 43 is a right-aligned triangle with fixed-width columns. Program 44 is a symmetric centered diamond with odd-length digit rows.
Printing a digit stays on the same line. Printing a newline ends the current line. Digits use cout without a newline; the row break uses cout << "\n" after the number loop.
O(n²) for n levels because each level prints O(n) digits and there are O(n) levels.
After cin >> levels, check failure and require levels ≥ 1 before the loops. Cap at 5 if you want single-digit values only.

Did you know?

This 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.

Next: X Pattern with Stars and Zeros

Continue with the next pattern in the C++ number-pattern series.

Program 45 tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful