Star Pyramid Daimond Pattern Program in C
#include<stdio.h>
int main()
{
int n;
printf("Enter number of lines: ");
scanf("%d", &n);
printf("\n");
// loop to print upper pyramid
for(int i = 1; i <= n; i++)
{
// loop to print leading spaces in each line
for(int space = 0; space <= n - i; space++)
{
printf(" ");
}
// loop to print *
for(int j = 1; j <= i * 2 - 1; j++)
{
printf(" * ");
}
printf("\n");
}
// loop to print lower pyramid
for(int i = n+1; i >= 1; i--)
{
// loop to print leading spaces in each line
for(int space = n-i; space >= 0; space--)
{
printf(" ");
}
// loop to print *
for(int j = i * 2 - 1; j >= 1; j--)
{
printf(" * ");
}
printf("\n");
}
return 0;
}
No comments:
Post a Comment