Q. Write a C program to print the square star pyramid design as:
****
* *
* *
****
Ans.
/*c program for star square design*/
#include<stdio.h>
int main()
{
int cols,rows,r,c,sp;
printf("Enter no. of columns: ");
scanf("%d", &cols);
printf("Enter no. of rows: ");
scanf("%d", &rows);
for(r=1; r<=cols; r++)
{
if(r==1 || r==cols)
printf(" ");
else
printf("*");
}
printf("
");
for(c=1; c<=rows-2; c++)
{
printf("*");
for(sp=1; sp<=cols-2; sp++)
printf(" ");
printf("*
");
}
for(r=1; r<=cols; r++)
{
if(r==1 || r==cols)
printf(" ");
else
printf("*");
}
getch();
return 0;
}
The output of above program would be:
****
* *
* *
****
Ans.
/*c program for star square design*/
#include<stdio.h>
int main()
{
int cols,rows,r,c,sp;
printf("Enter no. of columns: ");
scanf("%d", &cols);
printf("Enter no. of rows: ");
scanf("%d", &rows);
for(r=1; r<=cols; r++)
{
if(r==1 || r==cols)
printf(" ");
else
printf("*");
}
printf("
");
for(c=1; c<=rows-2; c++)
{
printf("*");
for(sp=1; sp<=cols-2; sp++)
printf(" ");
printf("*
");
}
for(r=1; r<=cols; r++)
{
if(r==1 || r==cols)
printf(" ");
else
printf("*");
}
getch();
return 0;
}
The output of above program would be:
Figure: Screen shot for Square Star Pyramid C program |
0 comments:
Post a Comment