Headlines
Loading...
C program which inputs n and r and computes nCr ie nCr = n! / r! (n-r)!

C program which inputs n and r and computes nCr ie nCr = n! / r! (n-r)!

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",&amp;n);
printf("Enter r");
scanf("%f",&amp;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==&gt; %.0f / (%.0f * %.0f)",fn,fr,fn_r);
printf("\n==&gt;nCr=%f",ncr);

getch();
}
float nfac(float n)
 {
 float i,fcn=1;
 for(i=1;i&lt;=n;i++)
  fcn*=i;
 return(fcn);
 }
float rfac(float r)
 {
 float i,fcr=1;
 for(i=1;i&lt;=r;i++)
  fcr*=i;
 return(fcr);
 }
float n_rfac(float n_r)
 {
 float i,fcn_r=1;
 for(i=1;i&lt;=n_r;i++)
  fcn_r*=i;
 return(fcn_r);
 }

/* Output
Enter n3
Enter r3

3C3=3!/(3!*0!)
==&gt; 6 / (6 * 1)
==&gt;nCr=1.000000
*/</conio></stdio>
*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***