Learn about C program to print hollow diamond of stars in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C program to print hollow diamond of stars
To print the pattern given below run the given C program. Give the required number of rows to print it.
* * * * * * * * * * * * * * * *
Program:
#include<stdio.h>
int main()
{
int n;
printf("Enter number of rows: ");
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
for(int j=i; j<=n; j++)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k==1 || k==(2*i-1)) printf("*");
else printf(" ");
}
printf("\n");
}
for(int i=n-1; i>=1; i--)
{
for(int j=n; j>=i; j--)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
if(k==1 || k==2*i-1) printf("*");
else printf(" ");
}
printf("\n");
}
return 0;
}
Output:
Enter number of rows: 5 * * * * * * * * * * * * * * * *
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to print full diamond of numbers, starting with zero and ending with zero
C program to print full diamond of numbers