Friday, 4 October 2013

C Program to calculate permutation formula - nPr

/* C Program to calculate permutation formula (nPr) */
 
#include <stdio.h>
#include <stdlib.h>
long npr(int n, int r);
long factorial(int x);
int main()



{
 
int n,r;
long npr_value;
printf("\nENTER THE VALUE OF n AND r : ");
scanf("%d%d",&n,&r);
npr_value=npr(n,r);
printf("\nVALUE = %ld",npr_value);
return 0;



}
 



/* Function to calculate the value of formula P(n,r)

input : the value of n and r

output : value of formula P(n,r)

*/
 

long npr(int n, int r)



{
 
long fact_n,fact_n_r,value;

fact_n = factorial(n);
fact_n_r = factorial(n-r);
if(fact_n_r==0)
{
printf("\n!! Indeterminant result because denominator is 0 ");
exit(0);
}
value = fact_n / fact_n_r;
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