C Concentric Number Square Pattern

Beginner
5 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 grid is symmetric horizontally.

Remember
Rule: for i from k down to 1
        left:  j = k..1, print (j > i ? j : i)
        right: j = 2..k, print (j > i ? j : i)

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

Follows the X pattern in Program 45; next is the full concentric diamond in Program 47.

How to Solve It

Start with fixed k = 5, then generalize with scanf — optionally demo k = 3 for paper tracing.

MethodIdeaBest for
Layer + mirrorOuter i = k..1; left k..1 then right 2..kLearning, interviews, exams
Ternary printprintf("%d ", j > i ? j : i)Compact demos and labs

Pseudocode

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

Cheat sheet

GoalPattern
Walk each layerfor (i = k; i >= 1; i--)
Left halffor (j = k; j >= 1; j--)
Right halffor (j = 2; j <= k; j++)
Cell valuej > i ? j : i
Row width2 * k - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d ", value)Stays on the same lineEach cell in both halves
printf("\n")Ends the current lineAfter both inner loops

Print all cells without a newline, then end the row once.

Live Preview

Change k and the concentric square updates instantly — capped at 9 so every value stays a single digit.

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

Live result k = 5 · 5×9 cells
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 — Row i = 3, k = 5

Trace the left half (j = 5..1) and right half (j = 2..5) for the middle layer.

Halfjj > i?Prints
Left5Yes5
Left4Yes4
Left3, 2, 1No3
Right2, 3No3
Right4, 5Yes4 / 5

Full row: 5 4 3 3 3 3 3 4 5. Width = 2k - 1 = 9; k rows → O(k²) cells.

C Programs

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

Example 1 — Fixed k = 5

Hard-coded outer value — if/else form of the j > i rule on both halves.

C
#include <stdio.h>

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

    for (i = k; i >= 1; i--)
    {
        for (j = k; j >= 1; j--)
        {
            if (j > i)
                printf("%d ", j);
            else
                printf("%d ", i);
        }

        for (j = 2; j <= k; j++)
        {
            if (j > i)
                printf("%d ", j);
            else
                printf("%d ", i);
        }

        printf("\n");
    }

    return 0;
}

How It Works

1. Layers shrink. i runs from 5 down to 1 — each pass is one concentric layer.

2. Left then right. Left prints k..1; right prints 2..k so the center digit is not doubled.

3. Cell rule. If j > i print the outer column value j; otherwise print the layer value i.

When i = 5 every cell is 5; when i = 1 the center is 1.

Example 2 — User Input k

Read k with scanf and reject non-positive values. Same logic with a ternary.

C
#include <stdio.h>

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

    printf("Enter k: ");
    if (scanf("%d", &k) != 1 || k <= 0)
    {
        printf("Please enter a positive integer.\n");
        return 1;
    }

    for (i = k; i >= 1; i--)
    {
        for (j = k; j >= 1; j--)
            printf("%d ", (j > i ? j : i));

        for (j = 2; j <= k; j++)
            printf("%d ", (j > i ? j : i));

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input with a clear message.

2. Same core. Width becomes 2k - 1 automatically from the two inner loops.

3. Safer input tip. Cap demos to single-digit layers:

Safer input
if (scanf("%d", &k) != 1 || k < 1 || k > 9)
{
    printf("Enter a whole number from 1 to 9.\n");
    return 1;
}

Example 3 — Compact k = 3

Same ternary loops with a smaller outer value for quick paper tracing.

C
#include <stdio.h>

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

    for (i = k; i >= 1; i--)
    {
        for (j = k; j >= 1; j--)
            printf("%d ", (j > i ? j : i));

        for (j = 2; j <= k; j++)
            printf("%d ", (j > i ? j : i));

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only k changes from 5 to 3 — both halves stay identical.

2. Trace on paper. For i = 2: left prints 3 2 2, right prints 2 3 → 3 2 2 2 3.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for k = 5 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

j = 1..k

Doubled center

If the right half starts at j = 1, the middle digit prints twice. Keep j = 2..k.

i++

Upside-down layers

Counting i up from 1 prints the center first. Use for (i = k; i >= 1; i--).

\n inside

Broken rows

If printf("\n") sits inside either half, each cell lands on its own line. Call the newline only after both halves.

j < i

Inverted layers

Swapping the ternary to j < i breaks the concentric look. Keep j > i ? j : i.

k = 1

Smallest square

Output is a single 1 — the right half does not run. A good sanity check.

scanf

Check the return value

If scanf fails, k may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(k²)O(1)
Compact k = 3 (Example 3)O(k²)O(1)

k rows × 2k - 1 cells each → about 2k² prints. For k = 5 that is 45 cells.

Key Takeaways

  • Rule: for each layer i, print j > i ? j : i on left k..1 then right 2..k.
  • Skip the center twice: the right half starts at 2 so the middle digit prints once.
  • Break the row: call printf("\n") only after both halves.
  • Complexity: O(k²) time from k × (2k - 1) cells; O(1) extra space.

One line: for i = k..1, print j > i ? j : i across left then right, then printf("\n").

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 left and right symmetrically.
Each row prints a left half (j = k..1) and a right half (j = 2..k) with the same j > i rule, mirroring values around the center.
When column j is still outside the current row layer i, print j (the outer number). Otherwise print i (the current row value).
The first loop builds the left descending half; the second loop mirrors columns 2..k on the right without repeating the center digit.
Change k (or read it from user input). Total width becomes 2*k - 1 — see Example 2.
O(k²) because each row prints roughly 2k - 1 cells and there are k rows.
Program 45 prints a star-and-zero X on a fixed grid. Program 46 prints decreasing/increasing numbers in a concentric square.
Each row has 2*k - 1 numbers. For k = 5, width is 9 columns.

Did you know?

Each cell prints j when j > i, else i. Row i runs from k down to 1; grid width = 2k - 1 columns per row.

Next: Concentric Number Diamond

Continue with the full diamond — top half k..1 plus bottom half 2..k.

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