C Number-Star Diamond Pattern

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

What Is This Pattern?

A number-star diamond grows from 1 to n*n*…*n, then mirrors back down. Each row alternates the row number and * using j % 2.

Remember
Rule: top i = 1..n, bottom i = n-1..1
      for j = 1..(2*i-1): * if j even, else digit i

1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1     ← n = 5

Two outer halves plus modulo alternation. Follows the right-aligned triangle in Program 30; next is Program 32.

How to Solve It

Start with fixed n = 5, then generalize with scanf — optionally demo a smaller height for tracing.

MethodIdeaBest for
Two halves + %Grow 1..n, shrink n-1..1; even j → *Learning, interviews, exams
Ternary printSame loops; (j % 2 == 0) ? printf("*") : printf("%d", i)Compact code after you know if/else

Pseudocode

Pseudocode
for i from 1 to n:
    for j from 1 to 2*i - 1:
        if j is even: print *
        else: print i
    print newline

for i from n-1 down to 1:
    (same inner loop)
    print newline

Cheat sheet

GoalPattern
Top halffor (i = 1; i <= n; i++)
Bottom halffor (i = n - 1; i >= 1; i--)
Row length 2*i-1for (j = 1; j < i * 2; j++)
Alternate digit / starif (j % 2 == 0) printf("*"); else printf("%d", i);
End the rowprintf("\n");

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", i) / printf("*")Stays on the same lineDigits and stars in the row
printf("\n")Ends the current lineAfter the inner loop

Print the whole row without a newline, then end the row once.

Live Preview

Change the diamond height and the pattern updates instantly — capped at 9 so every row number stays a single character.

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

Live result n 5 · 9 lines · 41 chars
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1

Worked Walkthrough — n = 3

Trace the top half (i = 1..3) and bottom half (i = 2..1). Each row prints 2*i - 1 characters.

HalfiChars (2*i-1)Printed row
Top111
Top232*2
Top353*3*3
Bottom232*2
Bottom111

Total lines = 2n - 1. Total characters = n² + (n - 1)² (here 9 + 4 = 13) — why time is O(n²).

C Programs

Three complete programs: fixed height, scanf input with ternary prints, and a small demo. Use View Output to reveal sample results.

Example 1 — Fixed n = 5

Hard-coded height — if/else with j % 2 in both halves.

C
#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 1; i <= 5; i++)
    {
        for (j = 1; j < i * 2; j++)
        {
            if (j % 2 == 0)
                printf("*");
            else
                printf("%d", i);
        }
        printf("\n");
    }

    for (i = 4; i >= 1; i--)
    {
        for (j = 1; j < i * 2; j++)
        {
            if (j % 2 == 0)
                printf("*");
            else
                printf("%d", i);
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Top half grows. i runs from 1 to 5 — rows get longer by two characters each time.

2. Inner loop alternates. Even j prints *; odd j prints the current row number i.

3. Bottom half mirrors. i runs from 4 down to 1 with the same inner logic — the peak is not repeated.

When i = 1 you get 1; when i = 3 you get 3*3*3.

Example 2 — User Input Version

Read n with scanf and clamp to 1–9. Ternary operators compact the digit-or-star choice.

C
#include <stdio.h>

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

    printf("Enter n (1-9): ");
    scanf("%d", &n);
    if (n < 1) n = 1;
    if (n > 9) n = 9;

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j < i * 2; j++)
            (j % 2 == 0) ? printf("*") : printf("%d", i);

        printf("\n");
    }

    for (i = n - 1; i >= 1; i--)
    {
        for (j = 1; j < i * 2; j++)
            (j % 2 == 0) ? printf("*") : printf("%d", i);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and clamp. Read n, then force it into 1..9 so row numbers stay single digits.

2. Same diamond core. Only the hard-coded 5/4 become n/n - 1; ternaries replace if/else.

3. Safer input tip. Unchecked scanf leaves n uninitialized on bad input. Prefer:

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

Example 3 — Compact n = 3

Same if/else logic with a smaller height for quick paper tracing.

C
#include <stdio.h>

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

    for (i = 1; i <= n; i++)
    {
        for (j = 1; j < i * 2; j++)
        {
            if (j % 2 == 0)
                printf("*");
            else
                printf("%d", i);
        }
        printf("\n");
    }

    for (i = n - 1; i >= 1; i--)
    {
        for (j = 1; j < i * 2; j++)
        {
            if (j % 2 == 0)
                printf("*");
            else
                printf("%d", i);
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only n changes from 5 to 3 — both halves and the modulo check stay identical.

2. Trace on paper. For i = 2: j = 1, 2, 3 → digit, star, digit → 2*2.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for the classic five-high diamond.

Edge Cases & Pitfalls

Check these before calling the solution done.

one loop

Missing bottom half

A single ascending outer loop only prints a triangle. You need the second loop i = n-1..1 for the diamond.

i = n twice

Doubled peak

Starting the bottom half at n instead of n - 1 prints the longest row twice. Begin at n - 1.

% flipped

Stars first

If you print * on odd j, rows start with a star. Keep digits on odd positions (j % 2 != 0).

\n inside

Broken rows

If printf("\n") sits inside the inner loop, each character lands on its own line. Call the newline only after the row finishes.

n > 9

Multi-digit values

printf("%d", 10) prints two characters and the diamond drifts. Clamp demos to 1–9.

n = 1

Smallest diamond

Output is a single 1 — the bottom loop never runs. A good sanity check.

Time and Space Complexity

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

Total characters = n² + (n - 1)². For n = 5 that is 25 + 16 = 41. Extra memory stays constant aside from loop counters.

Key Takeaways

  • Rule: top 1..n, bottom n-1..1; even j → *, odd j → digit i.
  • Row length: each row prints 2*i - 1 characters via j < i * 2.
  • Break the row: call printf("\n") only after the inner loop.
  • Complexity: O(n²) time from n² + (n-1)² chars; O(1) extra space.

One line: for each half, print 2*i-1 characters alternating digit i and *, then printf("\n").

Frequently Asked Questions

The inner loop runs while j < i*2, which prints 1, 3, 5, 7, 9 characters for i = 1..5.
It checks j % 2. Even j prints '*', odd j prints the current row number i.
The first loop builds the top half (i = 1..n). The second mirrors back down (i = n-1..1) to complete the diamond.
Program 30 is a right-aligned descending triangle. Program 31 alternates digits and stars in a symmetric diamond shape.
Replace 5 with n in both outer loops — see Example 2.
O(n²) for height n because total printed characters equal n² + (n-1)².
Check scanf's return value and reject values outside 1..9 so single-digit row numbers stay clear.
Yes — (j % 2 == 0) ? printf("*") : printf("%d", i) compacts the if/else logic.

Did you know?

This pattern prints a top half (1..n) and a bottom half (n-1..1). Each row prints 2*i-1 characters, alternating the row number and * using j % 2.

Next: Increasing Triangle from 11

Continue the C number-pattern series with a sequential triangle that starts at 11.

Program 32 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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