C Number Diamond Pattern

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

What Is This Pattern?

A centered number diamond grows odd-length digit rows up to a peak, then mirrors them back down. Each row prints 1 through 2*i-1 after leading spaces.

Remember
Rule: top  i = 1..levels
        bottom i = levels-1..1
      spaces = levels - i
      digits = 1..(2*i - 1)

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

Follows the right-aligned triangle in Program 43; next is Program 45.

How to Solve It

Start with fixed levels = 5, then generalize with scanf — optionally demo a smaller count for paper tracing.

MethodIdeaBest for
Top then bottomGrow i = 1..levels, then mirror levels-1..1Learning, interviews, exams
User input levelsSame loops; read levels with scanfReusable demos and labs

Pseudocode

Pseudocode
for i from 1 to levels:
    print (levels - i) spaces
    print k from 1 to (2*i - 1)
    print newline
for i from levels-1 down to 1:
    print (levels - i) spaces
    print k from 1 to (2*i - 1)
    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++) printf(" ");
Bottom spacesfor (j = levels; j > i; j--) printf(" ");
Digits on row ifor (k = 1; k < i * 2; k++) printf("%d", k);

Printing Numbers vs Starting a New Line

APIEffectUse for
printf(" ") / printf("%d", k)Stays on the same lineEach space and each digit
printf("\n")Ends the current lineAfter spaces + digits

Print spaces and digits without a newline, then end the row once.

Live Preview

Change the level count and the centered diamond updates instantly — capped at 5 so every digit stays a single character.

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

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

Worked Walkthrough — levels = 3

Trace every row of a small diamond — spaces, digit count, and printed output.

HalfiSpacesDigitsPrinted row
Top1211
Top211, 2, 3123
Top301..512345
Bottom211, 2, 3123
Bottom1211

Total lines = 2*levels - 1 (here 5). Digits grow as odd numbers and mirror — overall work is O(n²).

C Programs

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

Example 1 — Fixed levels = 5

Hard-coded diamond — top half grows to the peak, bottom half mirrors back down.

C
#include <stdio.h>

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

    for (i = 1; i <= 5; i++)
    {
        for (j = i; j < 5; j++)
            printf(" ");

        for (k = 1; k < i * 2; k++)
            printf("%d", k);

        printf("\n");
    }

    for (i = 4; i >= 1; i--)
    {
        for (j = 5; j > i; j--)
            printf(" ");

        for (k = 1; k < i * 2; k++)
            printf("%d", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Top half grows. i runs from 1 to 5 — each row gets one fewer space and two more digits.

2. Digits are odd-length. k runs while k < i * 2, so row i prints 2*i - 1 digits.

3. Bottom half mirrors. i runs from 4 down to 1 with the same digit rule and a matching space count.

When i = 1 you get four spaces and 1; when i = 3 you get 12345.

Example 2 — User Input Levels

Read levels with scanf and reject non-positive values. Same top/bottom core.

C
#include <stdio.h>

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

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

    for (i = 1; i <= levels; i++)
    {
        for (j = i; j < levels; j++)
            printf(" ");

        for (k = 1; k < i * 2; k++)
            printf("%d", k);

        printf("\n");
    }

    for (i = levels - 1; i >= 1; i--)
    {
        for (j = levels; j > i; j--)
            printf(" ");

        for (k = 1; k < i * 2; k++)
            printf("%d", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input with a clear message instead of looping on garbage.

2. Same core. Only the hard-coded 5 / 4 become levels and levels - 1.

3. Safer input tip. Cap demos so peak digits stay single characters:

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

Example 3 — Compact levels = 3

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

C
#include <stdio.h>

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

    for (i = 1; i <= levels; i++)
    {
        for (j = i; j < levels; j++)
            printf(" ");

        for (k = 1; k < i * 2; k++)
            printf("%d", k);

        printf("\n");
    }

    for (i = levels - 1; i >= 1; i--)
    {
        for (j = levels; j > i; j--)
            printf(" ");

        for (k = 1; k < i * 2; k++)
            printf("%d", k);

        printf("\n");
    }

    return 0;
}

How It Works

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

2. Trace on paper. For i = 2: one space, then 123 — the middle of the small diamond.

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

Edge Cases & Pitfalls

Check these before calling the solution done.

one half

Triangle instead of diamond

Skipping the bottom loop leaves only a pyramid. Keep both halves: 1..levels then levels-1..1.

i = levels..1

Duplicated peak

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

\n inside

Broken rows

If printf("\n") sits inside a digit loop, each digit lands on its own line. Call the newline only after spaces and digits.

levels > 5

Multi-digit values

When 2*i-1 >= 10, digits become two characters and the diamond drifts. Clamp demos to 1–5.

levels = 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, levels may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(levels²)O(1)
Small demo (Example 3)O(levels²)O(1)

About 2*levels - 1 rows, each printing O(levels) characters → O(n²). For levels = 5 that is 9 rows and 41 digits.

Key Takeaways

  • Rule: top 1..levels, bottom levels-1..1; each row prints levels - i spaces then 1..(2*i-1).
  • Odd widths: row length is always 2*i - 1 — that is what forms the diamond silhouette.
  • Break the row: call printf("\n") only after spaces and digits.
  • Complexity: O(n²) time from mirrored odd rows; O(1) extra space.

One line: for each half, print levels - i spaces then digits 1..(2*i-1), then printf("\n").

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.
Replace 5 with a levels variable in both outer loops and space bounds — see Example 2.
O(n²) for n levels because each level prints O(n) digits and there are O(n) levels (top and bottom).
Check scanf's return value and reject non-positive values so both outer loops have a valid range.

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 a grid that prints * on diagonals and 0 elsewhere.

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