Nested Pyramid

Q. Write a C program to print the following nested pyramid as:

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

Ans.

/*c program for nested pyramid*/
#include<stdio.h>
int main()
{
 int r,c,num=3,n;
 n=num;
 for(r=1; r<=num; r++,n--)
 {
   for(c=1; c<=r; c++)
       printf("*");
   printf(" ");

   for(c=n; c>=1; c--)
       printf("*");
   printf(" ");
   for(c=n; c>=1; c--)
       printf("*");
   printf(" ");
   for(c=1; c<=r; c++)
       printf("*");
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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

Comments