C Concentric Number Diamond Pattern

Beginner
5 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. Program 46 was the top half only — this page adds the bottom half.

Remember
Rule: top i = k..1, then bottom i = 2..k
      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
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

Follows the concentric square in Program 46; next is the powers-of-11 sequence in Program 48.

How to Solve It

Reuse Program 46’s cell logic, then add a second outer loop for the bottom half — skip i = 1 so the center row is not duplicated.

MethodIdeaBest for
Two outer loopsTop i = k..1, bottom i = 2..k; same left/right halvesLearning, interviews, exams
Ternary printprintf("%d ", j > i ? j : i)Compact demos and labs

Pseudocode

Pseudocode
for i from k down to 1:          // top half
    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

for i from 2 to k:               // bottom half
    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
Top halffor (i = k; i >= 1; i--)
Bottom halffor (i = 2; i <= k; i++)
Left halffor (j = k; j >= 1; j--)
Right halffor (j = 2; j <= k; j++)
Cell valuej > i ? j : i
Grid size2 * k - 1 rows and columns

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 full diamond 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 · 9×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 
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 — Row i = 3, k = 5

Trace the left half (j = 5..1) and right half (j = 2..5) for layer 3 — this row appears once in the top half and again in the bottom half.

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

Full row: 5 4 3 3 3 3 3 4 5. Grid = (2k - 1)² cells → O(k²).

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 — top half then bottom half for a complete 9×9 diamond.

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--)
            printf("%d ", (j > i ? j : i));

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

        printf("\n");
    }

    for (i = 2; i <= k; 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. Top half. i runs from 5 down to 1 — layers shrink to the center.

2. Bottom half. i runs from 2 up to 5 — skips i = 1 so the center row is not printed twice.

3. Same cell rule. Left k..1 then right 2..k; each cell is j > i ? j : 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. Both halves adjust automatically.

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

    for (i = 2; i <= k; 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. Grid becomes (2k - 1) × (2k - 1) automatically from the two outer 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 two-pass structure 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");
    }

    for (i = 2; i <= k; 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. Five rows total. Three from the top loop, two from the bottom (center already printed).

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.

i = 1 twice

Doubled center row

If the bottom loop starts at i = 1, the middle row prints twice. Keep for (i = 2; i <= k; i++).

missing bottom

Square instead of diamond

Omitting the second outer loop leaves only Program 46’s top half. Add i = 2..k after the center.

j = 1..k

Doubled center digit

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

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

k = 1

Smallest diamond

Output is a single 1 — the bottom loop 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)

(2k - 1) rows × (2k - 1) cells → about 4k² prints. For k = 5 that is 81 cells.

Key Takeaways

  • Two passes: top i = k..1, bottom i = 2..k — same left/right halves and j > i ? j : i rule.
  • Skip the center twice: bottom starts at 2; right half starts at 2 so neither the center row nor the middle digit repeats.
  • Break the row: call printf("\n") only after both halves.
  • Complexity: O(k²) time from (2k - 1)² cells; O(1) extra space.

One line: top i = k..1, bottom i = 2..k, cell rule j > i ? j : i, then printf("\n").

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) to mirror the shape without repeating the center row.
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 still outside the current row layer i, print j (the outer number). Otherwise print i (the current row value).
For k, each row has 2*k - 1 numbers. With k = 5, width is 9 columns and 9 rows.
Change the value of k or read it from user input — both outer loops and row width adjust automatically.
O(k²) because the grid has roughly (2k-1)² cells and each is printed once.
The center prints 1 — it is the deepest layer of the concentric pattern.

Did you know?

Print top half with for (i = k; i >= 1; i--), then bottom half with for (i = 2; i <= k; i++). Each cell: j > i ? j : i. Grid size = 2k - 1 rows and columns.

Next: Powers of 11 Pattern

Continue with the 1, 11, 121, 1331, 14641 sequence using a running multiply-by-11 state.

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