Tuesday, August 23, 2011

Generating Pascal Triangle - using combination formula

The formula for combination is ncr = n!/(r!*(n-r)!)

#include"stdio.h"
#include"conio.h"
int main()
{
    long int fact(int);
    int i,j,a,val;
    printf("Pascal Triangle");
    printf("\n Enter How many lines needed : ");
    scanf("%d",&a);
    for(i=0;i<=a;i++)
    {
        for(j=0;j<=i;j++)
        {
            val = fact(i)/(fact(j)*fact(i-j));
            printf("%d\t",val);
        }
        printf("\n");
    }

    return 1;
}
long int fact(int n)
{
    long int fa;
    if(n==0)
        return 1L;
    else
          fa = n * fact(n-1);
    return fa;
}

1 comment: