Learn about C program to print diamond star pattern at the center of the screen 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 diamond star pattern
To print a diamond start pattern in the middle of the screen use the below C code. It print the pattern at the center of the screen with required number of rows.
* *** ***** ******* ********* ******* ***** *** *
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=1; j<=40-i; j++)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
printf("*");
}
printf("\n");
}
for(int i=n-1; i>=1; i--)
{
for(int j=40; j>i; j--)
{
printf(" ");
}
for(int k=1; k<=2*i-1; k++)
{
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 :
Print the diamond pattern using recursion
C Program to Print Diamond Star Pattern