Print star pyramid pattern - C Program

Write a C program to generate following pattern for n lines:

Here, n=4 i.e 4 lines

star pyramid

Solution:

#include<stdio.h>
int main()
{
      int i,a,b;
      for(a=4, b=1; b<=4;a--,b++) // Here, a>=1 or b<=4 condition is optional. Both the condition execute the loop 4 times i.e n=4
      {
            for(i=1;i<a;i++)
                  printf(" ");
            for(i=1;i<=b;i++)
                  printf("* ");
            printf("\n");
      }
      return 0;
}


Output:

star pyramid