Problem
Statement
In
this program we will code a basic program to calculate NCR & NPR in C
Programming language.
Code
#include
int fact(int num)
{
int f=1;
int i;
for(i=1;i<=num;i++)
{
f=f*i;
}
return f;
}
int main()
{
int n,r;
int ncr,npr;
printf("nCr & nPr CALCULATOR :\n");
printf("ENTER THE VALUES OF n & r :\n");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
npr=fact(n)/(fact(n-r));
printf("%dC%d : %d\n",n,r,ncr);
printf("%dP%d : %d",n,r,npr);
return 0;
}
Output
nCr
& nPr CALCULATOR :
ENTER
THE VALUES OF n & r : 5 0
5C0
: 1
5P0
: 1
Explanation
In
this problem we have to generate the measure of nCr and nPr for given values of
'n' and 'r'. We have following formulas to calculate nCr and nPr respectively :
nCr = n! / r!*(n-r)!
nPr = n! /(n-r)!
Now after breaking this formula,we see that,all we have to do is find the factorial of each parameter i.e n,r,n-r. So,after finding the factorial,we put the values in formula.


0 Comments