Home » Sometech » WAP to calculate Binomial coefficient C(n,r)= n!/(r!*(n-r)!) using factorial function recursively

WAP to calculate Binomial coefficient C(n,r)= n!/(r!*(n-r)!) using factorial function recursively

#include <stdio.h>

 float fact(int n);

 

void main() {

    int n,r;

    float result;

    clrscr();

    printf(“\nEnter the value of n and r (0<=r<=n) \n”);

    scanf(“%d%d”,&n,&r);

    if(n<0||r<0||n<r)

      printf(“\nInvalid input\n”);

    else

    {

      result=fact(n)/(fact(r)*fact(n-r));

      printf(“\nC(%d,%d)=%.2f”,n,r,result);

    }

    getch();

}

/* function definition factorial recursively*/

float fact(int n)

{

   if(n==0)

     return(1);

   else

     return(n*fact(n-1));

}

Advertisements

SoME Tech

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

%d bloggers like this: