C Filled Diamond Star Pattern

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

What Is This Pattern?

A filled diamond star pattern is a centered solid diamond: an upper pyramid of odd-length star rows, mirrored below, with the widest row printed only once.

Remember
Rule: spaces = rows - i, stars = 2 * i - 1
      Grow i to rows, then shrink from rows - 1

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *       ← rows = 5 (half-height)

In C you print each row with two inner loops (spaces, then stars), then printf("\n"). Run that once upward and once downward so the shape closes into a diamond.

How to Solve It

Reuse one row formula for both halves — start with nested loops, then optionally extract a helper.

MethodIdeaBest for
Nested loopsSpaces loop + stars loop, twice (up then down)Learning, interviews, exams
Helper functionOne print_row called from both halvesLess duplication once loops click

Pseudocode

Pseudocode
for i from 1 to rows:           // upper half
    print (rows - i) spaces
    print (2 * i - 1) stars
    print newline

for i from rows - 1 down to 1:  // lower half
    print (rows - i) spaces
    print (2 * i - 1) stars
    print newline

Cheat sheet

GoalPattern
Upper halffor (i = 1; i <= rows; i++)
Leading spacesfor (j = 1; j <= rows - i; j++) printf(" ");
Odd star runfor (k = 1; k <= 2 * i - 1; k++) printf("*");
Lower halffor (i = rows - 1; i >= 1; i--)
Total lines2 * rows - 1
End the rowprintf("\n"); after both inner loops

Printing Stars vs Starting a New Line

APIEffectUse for
printf(" ") / printf("*")Stays on the same lineEach space and each *
printf("\n")Ends the current lineAfter spaces and stars for that row

Live Preview

Change the half-height and the diamond updates instantly — including line and star totals.

Whole numbers from 1 to 12. Printed lines = 2 * rows - 1.

Live result 5 half-height · 9 lines · 41 stars
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Worked Walkthrough — rows = 4

Trace spaces and stars for each i. Upper grows to the middle; lower shrinks from rows - 1.

HalfiSpacesStarsPrinted row
Upper131*
Upper223***
Upper315*****
Upper407*******
Lower315*****
Lower223***
Lower131*

Lines: 2×4 - 1 = 7. Stars: upper 1+3+5+7 = 16, lower 5+3+1 = 9, total 25 = 4² + 3².

C Programs

Three complete programs: fixed half-height, scanf input, and a reusable row helper. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded half-height — ideal for first demos and screenshots.

C
#include <stdio.h>

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

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= rows - i; j++)
        {
            printf(" ");
        }
        for (k = 1; k <= 2 * i - 1; k++)
        {
            printf("*");
        }
        printf("\n");
    }

    for (i = rows - 1; i >= 1; i--)
    {
        for (j = 1; j <= rows - i; j++)
        {
            printf(" ");
        }
        for (k = 1; k <= 2 * i - 1; k++)
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Set half-height. rows = 5 means the diamond peaks at 9 stars on the middle line — 9 total lines.

2. Upper half. i runs from 1 to rows. Print rows - i spaces, then 2 * i - 1 stars, then a newline.

3. Lower half. i runs from rows - 1 down to 1 with the same two inner loops — so the widest row is not repeated.

4. Break the line. printf("\n") after both inner loops starts the next row.

Example 2 — User Input Version

Read the half-height at runtime with scanf. Always check its return value in real apps (shown in the tip below).

C
#include <stdio.h>

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

    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= rows - i; j++)
        {
            printf(" ");
        }
        for (k = 1; k <= 2 * i - 1; k++)
        {
            printf("*");
        }
        printf("\n");
    }

    for (i = rows - 1; i >= 1; i--)
    {
        for (j = 1; j <= rows - i; j++)
        {
            printf(" ");
        }
        for (k = 1; k <= 2 * i - 1; k++)
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and read. Ask for a half-height, then store it with scanf("%d", &rows).

2. Same two-phase core. Only the source of rows changes — the print logic matches Example 1.

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

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

Example 3 — Helper Function

Extract one row printer so the diamond reads as “upper, then lower.”

C
#include <stdio.h>

void print_row(int rows, int i)
{
    int j, k;

    for (j = 1; j <= rows - i; j++)
        printf(" ");
    for (k = 1; k <= 2 * i - 1; k++)
        printf("*");
    printf("\n");
}

int main(void)
{
    int rows = 5;
    int i;

    for (i = 1; i <= rows; i++)
        print_row(rows, i);

    for (i = rows - 1; i >= 1; i--)
        print_row(rows, i);

    return 0;
}

How It Works

1. One helper for a row. print_row writes rows - i spaces, then 2 * i - 1 stars, then a newline.

2. Same outer structure. Call it upward for i = 1..rows, then downward from rows - 1.

3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

Lower at rows

Doubled middle

If the lower loop starts at rows, the widest line prints twice. Start at rows - 1.

Even stars

Lost center

Use 2 * i - 1 (odd counts). Even widths break left–right symmetry.

Wrong spaces

Not centered

Spaces must be rows - i. Using i spaces leans the diamond the wrong way.

\n inside

Broken rows

If printf("\n") sits inside a spaces or stars loop, the diamond collapses. Call it only after both inner loops.

rows = 1

Single star

Upper prints *; lower never runs — correct tiny diamond.

rows ≤ 0

Empty output

Both outer loops skip. Validate and re-prompt for interactive programs.

scanf

Check the return value

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

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
Helper function (Example 3)O(rows²)O(1)

About 2 * rows - 1 lines; each line does Θ(rows) work for spaces and stars. Total stars = rows² + (rows - 1)².

Key Takeaways

  • Row formula: rows - i spaces and 2 * i - 1 stars.
  • Two phases: grow i to rows, then shrink from rows - 1.
  • Break the row: printf for spaces/stars; printf("\n") after both inner loops.
  • Complexity: O(n²) time for half-height n; O(1) extra space.

One line: spaces = rows - i, stars = 2*i - 1, grow then shrink from rows - 1.

Frequently Asked Questions

The first outer loop runs i from 1 to rows. On each row it prints (rows - i) spaces, then (2 * i - 1) stars. That builds the upper centered pyramid. The second outer loop runs i from (rows - 1) down to 1 with the same two inner loops, mirroring the shape so the diamond closes.
The first part already prints the widest row when i equals rows. Starting the second part at rows - 1 continues with the next narrower rows without repeating the middle line.
The filled diamond prints full runs of stars using 2*i-1 stars per row. The hollow diamond uses diagonal conditions so only the outline has stars. Both use an upper phase and a lower phase starting at rows - 1.
Odd widths keep a single center star on each row and grow by one star on each side per step, which keeps left–right symmetry.
printf("*") or printf(" ") stays on the same line. printf("\n") ends the current line. Spaces and stars use printf without a newline; the row break uses printf("\n") after both inner loops.
With n equal to rows (half-height), there are 2n - 1 printed lines. Each line does Theta(n) work for spaces and stars combined, so overall time is O(n²).
Exactly 2 * rows - 1 lines. The widest line has 2 * rows - 1 stars.
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input, and require rows >= 1. Unchecked scanf leaves rows uninitialized on failure.

Did you know?

The filled diamond is Program 5’s pyramid plus its mirror: same (rows - i) spaces and (2 * i - 1) stars, with the lower half starting at rows - 1 so the widest row prints only once.

Next: Diamond in Square

Frame a hollow diamond inside solid top and bottom rows for Program 11.

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