10.14.2012

Star triangle frame pyramid

Q. Write a C program to print the following star structure
or
Q. Write a C program to print a triangle star frame.

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

Ans.

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

The output of above program would be:


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

3 comments:

  1. #include
    #include
    int main()
    {
    int i,j,k,l,m;
    for(i=1;i<=8;i++)
    {
    for(j=1;j<=i;j++)
    {
    if(j==i||j==1)
    printf("*");
    else
    printf(" ");
    }
    printf("\n");
    }
    for(j=1;j<8+2;j++)
    printf("*");
    }

    ReplyDelete