Hollow Diamond Star Pattern in C++

What You'll Learn
This pattern is Program 7 (inverted V, upper half) plus the lower half from the same diagonal logic as Program 8: after i runs 1…rows, run i from rows - 1 down to 1 with the same inner loops.
Each printed line has width 2 * rows - 1 (9 characters when rows = 5), including trailing spaces after the right edge.
⭐ Pattern Output
When you run the program with rows = 5:
*
* *
* *
* *
* *
* *
* *
* *
* Complete C++ Program
Fixed rows = 5 version:
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j, k;
for (i = 1; i <= rows; ++i) {
for (j = rows; j >= 1; --j) {
if (i == j) cout << "*";
else cout << " ";
}
for (k = 2; k <= rows; ++k) {
if (i == k) cout << "*";
else cout << " ";
}
cout << "\n";
}
for (i = rows - 1; i >= 1; --i) {
for (j = rows; j >= 1; --j) {
if (i == j) cout << "*";
else cout << " ";
}
for (k = 2; k <= rows; ++k) {
if (i == k) cout << "*";
else cout << " ";
}
cout << "\n";
}
return 0;
}🧠 How It Works
Upper half
for (i = 1; i <= rows; ++i) widens the hollow shape to the middle row. Each iteration streams the left j sweep then the right k sweep with cout.
Lower half
for (i = rows - 1; i >= 1; --i) repeats the same inner body so the legs close toward the bottom apex. Starting at rows - 1 skips printing the widest row twice.
Diagonal cells
j runs rows … 1; k runs 2 … rows. Star when i == j or i == k, otherwise cout << " ". Apex rows use only the j side for the single center star.
After every row
cout << "\n" ends each outer iteration in both halves. The inner block is duplicated in source so each phase stays a simple for loop.
Hollow diamond
2 × rows − 1 lines, each 2 × rows − 1 characters wide — O(rows²) output, O(1) extra space. Middle rows scroll horizontally in the preview on small screens.
Variation — User Input Version
Accept rows with cin:
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j, k;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; ++i) {
for (j = rows; j >= 1; --j) {
if (i == j) cout << "*";
else cout << " ";
}
for (k = 2; k <= rows; ++k) {
if (i == k) cout << "*";
else cout << " ";
}
cout << "\n";
}
for (i = rows - 1; i >= 1; --i) {
for (j = rows; j >= 1; --j) {
if (i == j) cout << "*";
else cout << " ";
}
for (k = 2; k <= rows; ++k) {
if (i == k) cout << "*";
else cout << " ";
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate
rows >= 1after reading withcin - Replace
*with#, digits, or custom symbols - Print only the first outer loop for an inverted hollow V (Program 7)
- For a filled diamond, you need rules for the interior (for example distance from the vertical center), not only the two diagonal equalities
i == jandi == k - Parameterize spacing if you want a wider gap between the two legs
Avoid
- Starting the second outer loop at
i == rows—you would duplicate the widest row - Claiming
i != j && i != kalone builds a filled diamond (it does not; it inverts the hollow logic incorrectly) - Dropping trailing spaces so columns no longer align to width
2 * rows - 1
Key Takeaways
Upper half = Program 7; lower half = same inners with i counting down from rows - 1.
Widest row appears once, when i == rows in the first loop.
Each row: left edge via j, right edge via k; same tests on both halves.
Line width is always 2 * rows - 1 characters.
Time complexity O(n²) for n = rows.
❓ Frequently Asked Questions
i from 1 to rows (hollow inverted V: from the top apex to the wide equator). The second runs i from rows - 1 to 1 (hollow upright V, closing to the bottom apex). Both use the same j / k loops and diagonal conditions.i == rows in the first loop. Starting the second loop at rows - 1 prevents printing that line twice.n rows: about 2n - 1 lines, each with two inner passes of length Θ(n).Next: Filled Diamond Pattern
Continue to Program 10 to print a filled diamond star pattern in C++.
You can build the hollow diamond by printing Program 7, then printing Program 8 without repeating the widest row (start the lower loop at rows - 1).
14 people found this page helpful
