Pascal Triangle In C Using For Loop Source Code


Enter the Limit :5                                                                                                                             
       1                                                                                                                                       
      1 1                                                                                                                                      
     1 2 1                                                                                                                                     
    1 3 3 1                                                                                                                                    
   1 4 6 4 1                                                                                                                                   
  1 5 10 10 5 1       

#include <stdio.h>
int fact(int);

int main()
{
    int i,j,n;
    
    printf("\nEnter the Limit :");
    scanf("%d", &n);
    for(i=0; i<=n;i++)
    {
        for(j=0;j<=n-i;j++) //To reduce the space
        {
            printf(" ");
        }
        for(j=0;j<=i;j++)
        {
            printf(" %d",fact(i)/(fact(j)*fact(i-j))); 
        }
        printf("\n");
    }

    return 0;
}

int fact(int n) //to find the factorial value. this will pass an integer value for main function
{
    int f=1,i;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    return(f);
}

Comments