Write a program which inputs n and r and computes nCr by calling the
function which returns the factorial of its argument. The function computes
the factorial by using recursive method.
nCr = n! / r! (n-r)!
#include<stdio .h="">
#include<conio .h="">
float nfac(float);
float rfac(float);
float n_rfac(float);
void main()
{
float n,r,n_r,fn=1,fr=1,fn_r=1,ncr;
clrscr();
printf("Enter n");
scanf("%f",&n);
printf("Enter r");
scanf("%f",&r);
n_r=n-r;
fn=nfac(n);
fr=rfac(r);
fn_r=n_rfac(n_r);
ncr=fn/(fr*fn_r);
printf("\n%.0fC%.0f=%.0f!/(%.0f!*%.0f!)",n,r,n,r,n_r);
printf("\n==> %.0f / (%.0f * %.0f)",fn,fr,fn_r);
printf("\n==>nCr=%f",ncr);
getch();
}
float nfac(float n)
{
float i,fcn=1;
for(i=1;i<=n;i++)
fcn*=i;
return(fcn);
}
float rfac(float r)
{
float i,fcr=1;
for(i=1;i<=r;i++)
fcr*=i;
return(fcr);
}
float n_rfac(float n_r)
{
float i,fcn_r=1;
for(i=1;i<=n_r;i++)
fcn_r*=i;
return(fcn_r);
}
/* Output
Enter n3
Enter r3
3C3=3!/(3!*0!)
==> 6 / (6 * 1)
==>nCr=1.000000
*/</conio></stdio>