Filled Diamond Star Pattern in C++

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n − 1 rows total

What You'll Learn

This program prints a filled diamond using two halves: the upper half increases stars by 2 each row, and the lower half decreases stars by 2, keeping the pattern centered with leading spaces.

For a given row index i, the star count is 2 * i - 1. Total output lines are 2 * rows - 1.

⭐ Pattern Output

When you run the program with rows = 5:

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

Complete C++ Program

Fixed rows = 5 version:

C++
#include <iostream>
using namespace std;

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

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

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

    return 0;
}

🧠 How It Works

1

Upper half

for (i = 1; i <= rows; ++i): for (j = 1; j <= rows - i; ++j) cout << " "; then for (j = 1; j <= 2 * i - 1; ++j) cout << "*"; — same reuse of j as the standalone pyramid.

Upper
2

Lower half

for (i = rows - 1; i >= 1; --i) runs the same two inner loops so odd widths step down (e.g. 7, 5, 3, 1 for rows = 5). No duplicate widest row.

Lower
3

Why 2 * i - 1?

Odd counts add one * on each side per row change, keeping the solid block symmetric about the vertical axis.

Center
4

Newline each row

cout << "\n" after both inner loops in each half. Total lines = 2*rows - 1; each line length follows (rows-i)+(2i-1).

Newline
=

Filled diamond

O(rows²) character writes, O(1) extra space. Widest rows scroll inside the touch-friendly green glyph on mobile.

2

Variation — User Input Version

Accept rows with cin:

C++
#include <iostream>
using namespace std;

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

    cout << "Enter the number of rows: ";
    cin >> rows;

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

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

    return 0;
}

💡 Tips for Enhancement

Try These

  • Validate rows >= 1 after reading with cin
  • 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.

Next: V Hollow in Box

Continue to Program 11 for a V-shaped hollow pattern inside a box.

Program 11 →
Did you know?

If you replace the star loop with diagonal checks (like Program 9), you can convert this filled diamond into a hollow diamond outline.

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.

10 people found this page helpful