nCr is an important function for mathematical operations. It’s mainly used in Binomial Distribution or Permutation and Combinations or Statistic Binomial Distribution. Now let’s see the mathematical expression for nCr
For example if we write 5C2 then this expression becomes
So, in our program we are going to calculate nCr. Now let’s write the code
For example if we write 5C2 then this expression becomes
So, in our program we are going to calculate nCr. Now let’s write the code
#include<iostream>
using namespace std;
int fact(int );
main()
{
int n,r;
float ncr,a,b;
cout<<"Insert the value of n";
cin>>n;
cout<<"Insert the value of r";
cin>>r;
if(r<n)
{
a=fact(n);
b=fact(n-r)*fact(r);
ncr=a/b;
cout<<"Answer is: "<<ncr;
}
else
{
cout<<"r must be less then n";
}
}
int fact(int i)
{ int m=1,j;
for(j=1;j<=i;j++)
{
m=m*j;
}
return(m);
}
Now let’s see the output of this programusing namespace std;
int fact(int );
main()
{
int n,r;
float ncr,a,b;
cout<<"Insert the value of n";
cin>>n;
cout<<"Insert the value of r";
cin>>r;
if(r<n)
{
a=fact(n);
b=fact(n-r)*fact(r);
ncr=a/b;
cout<<"Answer is: "<<ncr;
}
else
{
cout<<"r must be less then n";
}
}
int fact(int i)
{ int m=1,j;
for(j=1;j<=i;j++)
{
m=m*j;
}
return(m);
}
Insert the value of n 5
Insert the value of r 2
Answer is: 10
Insert the value of r 2
Answer is: 10




