Filled Diamond Star Pattern in C

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2*i - 1 stars per row

What You'll Learn

You combine a center-aligned pyramid (Program 5) with its mirror: same inner loops—(rows - i) spaces, then (2 * i - 1) stars—but after i reaches rows, run i from rows - 1 down to 1 (the same outer-loop idea as Program 6).

Unlike Program 9 (hollow diamond), rows are not padded to a fixed width: the tip row is shorter; the middle row has 2 * rows - 1 stars and no leading spaces when i == rows.

⭐ Pattern Output

When you run the program with rows = 5:

Output
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
1

Complete C Program

Fixed rows = 5 version (same j / k inner loops as Program 5):

c
#include <stdio.h>

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

    /* Upper half */
    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");
    }

    /* Lower half (no duplicate widest row) */
    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

Upper half (i = 1rows)

for (j = 1; j <= rows - i; ++j) printf(" "); then for (k = 1; k <= 2 * i - 1; ++k) printf("*"); — centered pyramid growing to the widest row.

Pyramid
2

Lower half (i = rows - 11)

Same two inner loops; outer i steps down so odd widths shrink (e.g. 7, 5, 3, 1 when rows = 5). Starting at rows - 1 avoids duplicating the widest row.

Mirror
3

Why 2 * i - 1?

Values 1, 3, 5, …, 2*rows-1 add one * on each side as i grows, so the solid block stays symmetric around the center column.

Odd counts
4

New line each row

printf("\n") closes every row in both halves. Total lines = 2*rows - 1; each line length is still (rows-i)+(2i-1)=rows+i-1.

println
=

Solid diamond

2*rows - 1 lines, each up to 2*rows - 1 characters wide — O(rows²) output, O(1) extra space. Widest rows scroll inside the green glyph on small screens.

2

Variation — User Input Version

Use scanf for rows:

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

💡 Tips for Enhancement

Try These

  • Validate rows >= 1 after scanf
  • For a hollow diamond, use Program 9 (diagonal j / k logic)—not a one-line tweak of the star loop
  • Print only the first outer loop to get Program 5’s pyramid alone
  • Swap characters or print row numbers inside the star run
  • Insert spaces between * characters for a looser diamond

Avoid

  • Starting the second outer loop at i == rows—duplicate widest row
  • Confusing this with Program 9: hollow diamonds need fixed width 2 * rows - 1 per line and different inner logic
  • Using i stars instead of 2 * i - 1—breaks centered symmetry

Key Takeaways

1

Upper half = Program 5; lower half = same inners with i from rows - 1 to 1.

2

Stars per row: 2 * i - 1; leading spaces: rows - i.

3

Widest row has 2 * rows - 1 stars; tip rows are shorter (no fixed line width).

4

Total lines: 2 * rows - 1.

5

Time complexity O(n²) for n = rows.

❓ Frequently Asked Questions

The first loop grows i and prints wider star runs each time. The second loop shrinks i and reuses the same space and star formulas, closing the diamond.
The row with 2 * rows - 1 stars is already printed when i == rows in the first loop. Continuing from rows - 1 avoids printing that line twice.
Here every position in the star segment is *. Program 9 only places stars on two diagonals and pads each line to length 2 * rows - 1.
O(n²) for n rows: about 2n - 1 lines, each with Θ(n) printing work in the two inner loops.

Explore More C Star Patterns!

Master Program 5 and Program 6, then this diamond is just both halves back-to-back.

All Star Patterns →
Did you know?

The filled diamond is the same nested-loop body as Programs 5 and 6; only the sequence of i values changes: up through rows, then down from rows - 1 without repeating the peak row.

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