A filled diamond is a solid, center-aligned diamond of * characters: an upper pyramid stacked with its mirror, for a total of 2 * rows - 1 lines.
Remember
Rule: spaces = rows - i, stars = 2 * i - 1
grow i to rows, then shrink from rows - 1
*
***
*****
*******
*********
*******
*****
***
* ← rows = 5 (9 lines)
Unlike the hollow diamond, every cell between the left and right edges is a star. The upper half is Program 5; the lower half reuses the same row body starting at rows - 1 so the widest line prints only once.
Approach
How to Solve It
Two ways to emit the same shape — start with nested loops, then optionally shorten with a helper.
Method
Idea
Best for
Two-phase nested loops
Upper 1..rows, lower rows-1..1; spaces then stars
Learning, interviews, exams
print_row helper
Encode the shared formula once; both phases call it
Shorter demos once formulas 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
Goal
Pattern
Leading spaces
for (j = 1; j <= rows - i; ++j) cout << " ";
Odd star run
for (k = 1; k <= 2 * i - 1; ++k) cout << "*";
Upper half
for (i = 1; i <= rows; ++i)
Lower half
for (i = rows - 1; i >= 1; --i)
End the row
cout << "\n";
Total lines
2 * rows - 1
Helper shortcut
cout << string(rows - i, ' ') << string(2 * i - 1, '*') << "\n";
Printing Stars vs Starting a New Line
API
Effect
Use for
cout << " " / "*"
Stays on the same line
Each space and each star
cout << "\n"
Ends the current line
After both inner loops
Try it
Live Preview
Change the half-height and the filled diamond updates instantly — including line and star totals.
Whole numbers from 1 to 12. Total lines = 2 * rows - 1.
Live result5 half · 9 lines · 41 stars
*
***
*****
*******
*********
*******
*****
***
*
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop i: spaces, stars, and which half produced the line.
Half
i
Spaces
Stars
Printed row
Upper
1
3
1
*
Upper
2
2
3
***
Upper
3
1
5
*****
Upper
4
0
7
*******
Lower
3
1
5
*****
Lower
2
2
3
***
Lower
1
3
1
*
Total lines: 2 × 4 - 1 = 7. The widest row (i = 4) appears only in the upper half — that is why time is still O(n²).
Code
C++ Programs
Three complete programs: fixed half-height, console input, and a print_row helper. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded half-height — upper pyramid, then mirrored lower half without duplicating the middle.
C++
#include <iostream>
using namespace std;
int main() {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= rows - i; ++j) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << "*";
}
cout << "\n";
}
for (int i = rows - 1; i >= 1; --i) {
for (int j = 1; j <= rows - i; ++j) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << "*";
}
cout << "\n";
}
return 0;
}
Output
*
***
*****
*******
*********
*******
*****
***
*
How It Works
1. Set half-height.rows = 5 means 9 diamond lines (2 * 5 - 1).
2. Upper half grows.i runs from 1 to rows: spaces shrink, stars grow 1, 3, 5, 7, 9.
3. Lower half shrinks.i runs from rows - 1 down to 1 with the same space/star formulas.
4. Break each line.cout << "\n" after both inner loops starts the next row.
Starting the lower loop at 4 (not 5) avoids reprinting the nine-star middle row.
Example 2 — User Input Version
Read the half-height at runtime. Check cin.fail() in real apps (shown in the tip below).
C++
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= rows - i; ++j) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << "*";
}
cout << "\n";
}
for (int i = rows - 1; i >= 1; --i) {
for (int j = 1; j <= rows - i; ++j) {
cout << " ";
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << "*";
}
cout << "\n";
}
return 0;
}
Output (when user enters 4)
Enter the number of rows: 4
*
***
*****
*******
*****
***
*
How It Works
1. Prompt and read. Ask for a half-height, then store it with cin >> rows.
2. Same two-phase core. Only the source of rows changes — the space and star logic matches Example 1.
if (!(cin >> rows) || rows < 1) {
cout << "Enter a positive whole number.\n";
return 1;
}
Example 3 — print_row Helper
Encode the shared space/star formula once; both outer loops call it.
C++
#include <iostream>
#include <string>
using namespace std;
void print_row(int rows, int i) {
cout << string(rows - i, ' ')
<< string(2 * i - 1, '*')
<< "\n";
}
int main() {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
print_row(rows, i);
}
for (int i = rows - 1; i >= 1; --i) {
print_row(rows, i);
}
return 0;
}
Output
*
***
*****
*******
*********
*******
*****
***
*
How It Works
1. One shared formula.print_row builds the margin and odd star run with std::string.
2. Same two phases. Upper loop still grows; lower loop still starts at rows - 1.
3. Less duplication. Changing the row body means editing one function instead of four inner loops.
Learn the nested-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
Lower = rows
Duplicate middle
Starting the lower loop at rows reprints the widest line. Use rows - 1.
Tabs
Broken centering
Always print the space character " ", not tabs — tab width varies and skews the diamond.
Newline inside
Column of stars
If cout << "\n" is inside either inner loop, each character lands on its own line. End the row only after both loops.
rows = 1
Single tip star
Upper prints *; lower never runs. Output is one star — a good sanity check.
rows ≤ 0
Empty output
Both loops never run. Validate and re-prompt for interactive programs.
Bad input
Check cin.fail()
Letters leave cin in a failed state — prefer if (!(cin >> rows) || rows < 1).
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
print_row helper (Example 3)
O(rows²)
O(rows) temporary per row string
About 2n - 1 lines; each prints up to 2n - 1 characters — still quadratic in n. Star total = n² + (n - 1)².
Remember
Key Takeaways
Rule: spaces = rows - i, stars = 2 * i - 1.
Two phases: grow to rows, then shrink from rows - 1.
Break the row: call cout << "\n" only after both inner loops.
Complexity:O(n²) time; O(1) extra space for nested loops.
One line: print a centered odd-width row for i = 1..rows, then the same for i = rows-1..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 upper half already prints the widest row when i equals rows. Starting the lower half at rows - 1 continues with the next narrower rows without repeating the middle line.
The filled diamond prints full runs of stars with 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.
cout << "*" stays on the same line. cout << "\n" ends the current line. Spaces and stars use cout without a newline; the row break uses cout << "\n" after both inner loops.
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.
O(n²) for n equal to rows. There are about 2n - 1 printed rows, and each row does Theta(n) work for spaces and stars combined.
Yes. The upper half is Program 5; the lower half uses the same inner loops with i running like Program 6’s inverted idea, but starting at rows - 1.
After cin >> rows, check cin.fail() and require rows >= 1 so bad input does not leave rows unset.
🤔
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.