Hollow Diamond Pattern in C using For loop
ADVERTISEMENTS
Hollow diamond pattern in C using for loop. In this article, you will learn how to print the hollow diamond pattern in c using for loop.
Source Code
// Hollow Diamond Pattern in C using For loop
#include <stdio.h>
int hollowDiamond(int x);
// It's the driver function
int main() {
int x;
// x - denotes the size of the hollow diamond
printf("-----Enter the size of the hollow diamond, it should be even-----\n");
scanf("%d", &x);
// Size of the hollow diamond should be even number
if (x % 2 == 1)
x++;
// This will print the hollow diamond pattern
hollowDiamond(x);
return 0;
}
// It's the function to generate hollow pattern
int hollowDiamond(int size) {
int i, j, diff;
diff = size / 2;
printf("\n-----The hollow diamond pattern is-----\n\n");
// This will print the first half diamond
for (i = 1; i <= diff; i++) {
printf("\t");
for (j = 1; j <= diff - i; j++) {
printf(" ");
}
if (i == 1) {
printf("*");
}
else {
printf("*");
for (j = 1; j <= 2 *i - 3; j++)
{
printf(" ");
}
printf("*");
}
printf("\n");
}
// This will print the last half diamond
for (i = diff + 1; i < size; i++) {
printf("\t");
for (j = 1; j <= i - diff; j++) {
printf(" ");
}
if (i == size - 1) {
printf("*");
}
else {
printf("*");
for (j = 1; j <= 2 *(size - i) - 3; j++) {
printf(" ");
}
printf("*");
}
printf("\n");
}
}
Output
-----Enter the size of the hollow diamond, it should be even-----
15
-----The hollow diamond pattern is-----
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*