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 <stdio.h>
void printHollowDiamond(int numRows) {
int row, space, star;
for (row = 1; row <= numRows; row++) {
for (space = 1; space <= numRows - row; space++) {
printf(" ");
}
for (star = 1; star <= 2 * row - 1; star++) {
if (star == 1 || star == 2 * row - 1) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
for (row = numRows - 1; row >= 1; row--) {
for (space = 1; space <= numRows - row; space++) {
printf(" ");
}
for (star = 1; star <= 2 * row - 1; star++) {
if (star == 1 || star == 2 * row - 1) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
}
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.