Sunday, November 18, 2012

Star Pyramid

Q. Write a C program to accept the number of rows of pyramid from user and print the correspondence star triangle.
For example,

Enter number by user : 7

*
**
***
****
***
**
*

Ans.

/*c program for star pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 printf("Enter number of rows : ");
 scanf("%d", &num);
 for(r=1; r<=(num/2)+1; r++)
 {
  for(c=1; c<=r; c++)
    printf("*");
  printf("\n");
 }
 for(r=1; r<=(num/2); r++)
 {
  for(c=r; c<=num/2; c++)
    printf("*");
  printf("\n");
 }
 return 0;
}

The output of above program would be:

Output of star pyramid C program
Figure: Screen shot for star pyramid C program

No comments:

Post a Comment