Problem in output when nCr fuction is created using factorial function

61 Views Asked by At
#include <iostream>
using namespace std;

int fact(int n)
{
    if (n == 0)
    {
        return 1;
    }
    else
    {
        return fact(n - 1) * n;
    }
}

int ncr(int n, int r)
{
    int a = fact(n);
    int b = fact(n - r) * fact(r);

    return a / b;
}

int main()
{
    cout << "ncr(6,3) is : " << ncr(6, 3) << endl;
    cout << "ncr(13,4) is : " << ncr(13, 4) << endl;
    return 0;
}
ncr(6,3) is : 20
ncr(13,4) is : 221

The output till n=12 is correct, but after that when n > 12 the output is mathematically incorrect.

What is the problem? There is no error shown in VS Code.

0

There are 0 best solutions below