C++ Concentric Number Diamond Pattern

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

What Is This Pattern?

A concentric number diamond peels layers from outer value k down to 1 at the center, then mirrors back out to k — a square grid of size 2k - 1 on each side.

Remember
Rule: same cell as Program 46 — (j > i ? j : i)
Plus a bottom half: i = 2..k after the top half

5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5     ← k = 5

Unlike Program 46 (top half only), this shape adds a second outer loop so the diamond is complete and symmetric.

How to Solve It

Reuse Program 46’s row builder twice: peel i = k..1, then mirror i = 2..k.

MethodIdeaBest for
Two outer loopsTop k..1, bottom 2..k, same inner halvesLearning, interviews, demos
Ternary cellscout << (j > i ? j : i) in both halvesCompact code once the rule clicks

Pseudocode

Pseudocode
for i from k down to 1:          // top half
    print left half and right half with (j > i ? j : i)
    print newline
for i from 2 to k:               // bottom half (skip center)
    print left half and right half with (j > i ? j : i)
    print newline

// left:  j from k down to 1
// right: j from 2 to k

Cheat sheet

GoalPattern
Top halffor (i = k; i >= 1; i--)
Bottom halffor (i = 2; i <= k; i++)
Left / rightj = k..1 / j = 2..k
Cell valuej > i ? j : i
Print cellcout << value << " "
End the rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << n << " "Stays on the same lineEach cell
cout << "\n"Ends the current lineAfter both inner loops

Print cells 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 outer value k and the full concentric diamond updates instantly.

Use k from 2 to 7. Tap a chip or type a value — the preview redraws as you go.

Live result k = 5 · 9×9
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5

Worked Walkthrough

Same row rule as Program 46. Here is how the two outer loops build the diamond for k = 5.

Passi valuesWhat appears
Top5, 4, 3, 2, 1outer → center (ends with 1)
Bottom2, 3, 4, 5mirror out (skips i = 1)
Sample rowi = 35 4 3 3 3 3 3 4 5

Starting the bottom loop at 2 avoids printing the center row twice. Cell rule stays j > i ? j : i on both left and right halves.

C++ Programs

Three complete programs: fixed k = 5, cin input, and a compact k = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed k = 5

Hard-coded outer value — top half then bottom half for a complete 9×9 diamond.

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

int main()
{
    int k = 5;
    int i, j;

    for (i = k; i >= 1; i--)
    {
        for (j = k; j >= 1; j--)
            cout << (j > i ? j : i) << " ";
        for (j = 2; j <= k; j++)
            cout << (j > i ? j : i) << " ";
        cout << "\n";
    }

    for (i = 2; i <= k; i++)
    {
        for (j = k; j >= 1; j--)
            cout << (j > i ? j : i) << " ";
        for (j = 2; j <= k; j++)
            cout << (j > i ? j : i) << " ";
        cout << "\n";
    }

    return 0;
}

How It Works

1. Top half peels inward. i runs from 5 down to 1 — same row builder as Program 46.

2. Bottom half mirrors out. i runs from 2 to 5 so the center row is not printed twice.

3. Same cell rule everywhere. Left and right halves always use (j > i ? j : i), then "\n" ends the row.

Example 2 — User Input k

Read k at runtime — both halves scale automatically.

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

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

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

    for (i = k; i >= 1; i--)
    {
        for (j = k; j >= 1; j--)
            cout << (j > i ? j : i) << " ";
        for (j = 2; j <= k; j++)
            cout << (j > i ? j : i) << " ";
        cout << "\n";
    }

    for (i = 2; i <= k; i++)
    {
        for (j = k; j >= 1; j--)
            cout << (j > i ? j : i) << " ";
        for (j = 2; j <= k; j++)
            cout << (j > i ? j : i) << " ";
        cout << "\n";
    }

    return 0;
}

How It Works

1. Same two-pass structure. Only the source of k changes from a literal to cin.

2. Grid size follows k. Entering 4 yields a 7 × 7 diamond (2k - 1).

3. Validate in real apps. Prefer checking cin failure and requiring k >= 1 (tip below).

Safer input tip
if (!(cin >> k) || k < 1)
{
    cout << "Please enter a positive integer.\n";
    return 1;
}

Example 3 — Compact k = 3

Smaller outer value for quick tracing — five rows, easy to dry-run.

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

int main()
{
    int k = 3;
    int i, j;

    for (i = k; i >= 1; i--)
    {
        for (j = k; j >= 1; j--)
            cout << (j > i ? j : i) << " ";
        for (j = 2; j <= k; j++)
            cout << (j > i ? j : i) << " ";
        cout << "\n";
    }

    for (i = 2; i <= k; i++)
    {
        for (j = k; j >= 1; j--)
            cout << (j > i ? j : i) << " ";
        for (j = 2; j <= k; j++)
            cout << (j > i ? j : i) << " ";
        cout << "\n";
    }

    return 0;
}

How It Works

1. Five rows total. Top prints three rows (i = 3..1); bottom adds two more (i = 2..3).

2. Same formula. Nothing changes except k — proving the pattern scales.

3. Center stays 1. The middle row is always the deepest layer before the mirror begins.

Edge Cases & Pitfalls

Check these before calling the solution done.

Bottom starts at 2

Do not start the bottom loop at 1

Starting at 1 duplicates the center row. Use for (i = 2; i <= k; i++).

Missing bottom

Forgetting the second outer loop

That is Program 46 (square / top half only). Add the bottom pass for the full diamond.

Right starts at 2

Do not start the right half at 1

Starting at 1 duplicates the center cell on every row.

Bad cin

Validate input

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

Time and Space Complexity

ProgramTimeExtra space
Full diamond (Examples 1–3)O(k²)O(1)

The grid is (2k - 1) × (2k - 1), so work is quadratic in k. Only a few integers of extra memory.

Key Takeaways

  • Rule: print j when j > i, otherwise print i.
  • Two passes: top i = k..1, bottom i = 2..k — size 2k - 1.
  • Break the row: call cout << "\n" only after both inner loops.
  • Complexity: O(k²) time; O(1) extra space.

One line: print Program 46’s square for i = k..1, then repeat for i = 2..k to finish the diamond.

Frequently Asked Questions

A full concentric number diamond where the outer layer is k (e.g. 5), values decrease to 1 at the center, then increase back to k — a 9×9 grid when k = 5.
The first prints the top half (i = k down to 1). The second prints the bottom half (i = 2 up to k) so the center row is not duplicated.
Program 46 prints only the top half (k rows). Program 47 adds the bottom half loop to form a complete diamond.
When column j is outside the current layer i, print j (outer ring). Otherwise print i (inner plateau).
Each row has 2*k - 1 numbers. With k = 5, the grid is 9 columns and 9 rows.
Printing a number (and space) stays on the same line. Printing a newline ends the current line. Cells use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(k²) because the grid has (2k-1)² cells and each is printed once.
The center prints 1 — the deepest layer of the concentric pattern.

Did you know?

Top half: i = k..1. Bottom half: i = 2..k (skip center). Grid size = (2k - 1) × (2k - 1) — for k = 5 that is 9 × 9 = 81 prints.

Next: Powers of 11 Sequence

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

Program 48 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