C++ Concentric Number Square Pattern

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

What Is This Pattern?

A concentric number square peels layers from outer value k down to 1 at the center. Each row mirrors left and right so the shape stays symmetric — width 2k - 1, height k (top half only).

Remember
Rule: print (j > i ? j : i) on left and right halves

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     ← k = 5

Unlike Program 45 (star-and-zero X), this shape uses number layers, not diagonal checks.

How to Solve It

Walk layers i = k..1. On each layer, fill left then right with the j > i rule.

MethodIdeaBest for
Two inner loopsLeft j = k..1, right j = 2..k, same cell ruleLearning, interviews, demos
Ternary formcout << (j > i ? j : i) instead of if-elseCompact code once the rule clicks

Pseudocode

Pseudocode
for i from k down to 1:
    for j from k down to 1:
        print (j > i ? j : i) and a space
    for j from 2 to k:
        print (j > i ? j : i) and a space
    print newline

Cheat sheet

GoalPattern
Outer layersfor (i = k; i >= 1; i--)
Left halffor (j = k; j >= 1; j--)
Right halffor (j = 2; j <= k; j++)
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 concentric square updates instantly.

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

Live result k = 5 · 5×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

Worked Walkthrough

Trace the middle row when k = 5 and i = 3 — left half then right half.

Halfjj > i?Print
Left5, 4, 3, 2, 1yes, yes, no, no, no5 4 3 3 3
Right2, 3, 4, 5no, no, yes, yes3 3 4 5
Full row5 4 3 3 3 3 3 4 5

When j > 3, print j (outer ring). When j <= 3, print i (inner plateau of threes). The right loop starts at 2 so the center cell is not duplicated.

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 — ideal for first demos and screenshots.

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--)
        {
            if (j > i)
                cout << j << " ";
            else
                cout << i << " ";
        }

        for (j = 2; j <= k; j++)
        {
            if (j > i)
                cout << j << " ";
            else
                cout << i << " ";
        }

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop peels layers. i runs from 5 down to 1 — one row per layer.

2. Left half fills to the center. j counts from k down to 1; print j when j > i, else i.

3. Right half mirrors out. j runs from 2 to k with the same rule, then "\n" ends the row.

Example 2 — User Input k

Read k at runtime and use the ternary form of the cell rule.

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

    return 0;
}

How It Works

1. Same core loops. Only the source of k changes from a literal to cin.

2. Ternary replaces if-else. (j > i ? j : i) is exactly the rule from Example 1.

3. Width scales automatically. Entering 3 yields width 5 (2k - 1).

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 on paper or in interviews.

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

    return 0;
}

How It Works

1. Only three rows. Easy to dry-run every j value by hand.

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

3. Center is always 1. The last row always ends with ... 1 ... when the loops finish at i = 1.

Edge Cases & Pitfalls

Check these before calling the solution done.

Right starts at 2

Do not start the right loop at 1

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

j > i

Do not swap the comparison

j > i picks the outer ring. Using j < i or i > j alone without care inverts the layers.

Bad cin

Validate input

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

Spaces

Print a space after each value

Use cout << value << " " so columns stay readable and match the sample layout.

Time and Space Complexity

ProgramTimeExtra space
Nested layer loops (Examples 1–3)O(k²)O(1)

There are k rows and each prints 2k - 1 values, 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 halves: left j = k..1, right j = 2..k — width 2k - 1.
  • Break the row: call cout << "\n" only after both inner loops.
  • Complexity: O(k²) time; O(1) extra space.

One line: for each layer i, print left then right with (j > i ? j : i), then end the line.

Frequently Asked Questions

A concentric number square where the outer layer is k (e.g. 5) and numbers decrease toward the center, ending with 1, then mirror back out on each row.
For the current layer i, if the column index j is greater than i, print j (outer ring); otherwise print i (fill the inner plateau).
The first walks left-to-center (j = k down to 1). The second walks center-to-right (j = 2 to k), skipping a second copy of the center cell.
Width is 2k - 1. For k = 5 the row has 9 numbers; for k = 3 it has 5.
Program 45 prints * and 0 on diagonals. Program 46 prints decreasing/increasing numbers in concentric layers.
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 each of k rows prints 2k - 1 values.
After cin >> k, check failure and require k ≥ 1 before the loops.

Did you know?

Each row has width 2k - 1. The outer loop runs k times (i = k..1). Total prints = k × (2k - 1) — quadratic in k.

Next: Concentric Number Diamond

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

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