To print a Hollow Diamond Pattern in C++, we use loops and conditional statements to manage spaces and stars.
In this example,
// Print Hollow Diamond Pattern in C++
#include <iostream>
void printHollowDiamond(int numRows) {
int row, space, star;
for (row = 1; row <= numRows; row++) {
for (space = 1; space <= numRows - row; space++) {
std::cout << " ";
}
for (star = 1; star <= 2 * row - 1; star++) {
if (star == 1 || star == 2 * row - 1) {
std::cout << "*";
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
for (row = numRows - 1; row >= 1; row--) {
for (space = 1; space <= numRows - row; space++) {
std::cout << " ";
}
for (star = 1; star <= 2 * row - 1; star++) {
if (star == 1 || star == 2 * row - 1) {
std::cout << "*";
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
}
int main() {
int numRows = 5;
printHollowDiamond(numRows);
return 0;
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Diamond Pattern in C++ language with well detailed examples.