I have a code which calculate binomial coefficient, but when number is bigger then 20, it start to calculate wrong, where is the problem? Thanks
#include <iostream>
using namespace std;
long int bin(long int x)
{
if(x==0)
return 1;
long int r = x;
for(int i = r-1;i>0;i--)
{
r = r*i;
}
return r;
}
int main()
{
cout << "Write n and k: " << endl;
long int n=0;
long int k=0;
cin >> n;
cin >> k;
long int result = 0;
long int fn = bin(n);
long int fk = bin(k);
long int fnk = bin(n-k);
result = fn/(fk*fnk);
cout << endl << "C = " << result << endl;
return 0;
}
for example 12 and 5 = 792 which is correct, but 20 and 4 = -2 which is not correct