/* C Program to calculate combination formula nCr */
#include <stdio.h>
#include <stdlib.h>
long ncr(int n, int r);
long factorial(int x);
int main()
{
int n,r;
long ncr_value;
printf("\nENTER THE VALUE OF n AND r : ");
scanf("%d%d",&n,&r);
ncr_value=ncr(n,r);
printf("\nVALUE = %ld",ncr_value);
return 0;
}
/* Function to calculate the value of formula C(n,r)
input : the value of n and r
output : value of formula C(n,r)
*/
long ncr(int n, int r)
{
long fact_n,fact_n_r,fact_r,value,dn;
fact_n = factorial(n);
fact_n_r = factorial(n-r);
fact_r = factorial(r);
dn=fact_n_r * fact_r;
if(dn==0)
{
printf("!! Indeterminant result !!");
exit(0);
}
else
value = fact_n / dn;
return(value);
}
/* function to calculate the factorial
Input : a number
output : factorial value of that number
*/
long factorial(int x)
{
int i;
long fact=1;
if(x<0)
return 0;
for(i=x;i>=2;i--)
fact=fact*i;
return(fact);
}
#include <stdio.h>
#include <stdlib.h>
long ncr(int n, int r);
long factorial(int x);
int main()
{
int n,r;
long ncr_value;
printf("\nENTER THE VALUE OF n AND r : ");
scanf("%d%d",&n,&r);
ncr_value=ncr(n,r);
printf("\nVALUE = %ld",ncr_value);
return 0;
}
/* Function to calculate the value of formula C(n,r)
input : the value of n and r
output : value of formula C(n,r)
*/
long ncr(int n, int r)
{
long fact_n,fact_n_r,fact_r,value,dn;
fact_n = factorial(n);
fact_n_r = factorial(n-r);
fact_r = factorial(r);
dn=fact_n_r * fact_r;
if(dn==0)
{
printf("!! Indeterminant result !!");
exit(0);
}
else
value = fact_n / dn;
return(value);
}
/* function to calculate the factorial
Input : a number
output : factorial value of that number
*/
long factorial(int x)
{
int i;
long fact=1;
if(x<0)
return 0;
for(i=x;i>=2;i--)
fact=fact*i;
return(fact);
}
No comments:
Post a Comment