Home > database >  code to find the value of nCr shows the answer to some values as 0( Ex:30 15)
code to find the value of nCr shows the answer to some values as 0( Ex:30 15)

Time:07-25

The following is the code: #include using namespace std;

int factorial(int num){       
unsigned long long int fact=1;
for (int i = num; i >=1; i--)
{
    fact=fact*i;
}
return fact;
}

int main()
{
unsigned long long int n,r,value;
cout<<"Enter a number whose nCr value is to be calculated (n and r respectively): ";
cin>>n>>r;
 unsigned long long int a=factorial(n);
 unsigned long long int b=factorial(r);
 unsigned long long int c=factorial(n-r);
 value=a/(b*c);
 cout<<"The value of nCr is : "<<value;
 return 0;
}

Why do I get the answer to some of the inputs like (30 15),(30 12), etc as 0.

CodePudding user response:

30! is a very large 33-digit number, so that's overflowing the int variable your program is trying to store it in. If you print it out, you'll see the actual value that gets stored in a is smaller than the value of b*c in the denominator of the final computation, so value=a/(b*c); gets truncated to 0 by integer division.

Even if you return the result of factorial as an unsigned long long int the result of 30! will overflow, since it can only store 64 bits (and that's compiler dependent).

#include "stdafx.h"
#include <iostream>

unsigned long long int factorial(int num) {
    unsigned long long int fact = 1;
    for (int i = num; i >= 1; i--)
    {
        fact = fact * i;
    }
    return fact;
}


int main()
{
    unsigned long long int n, r, value;
    std::cout << "Enter a number whose nCr value is to be calculated (n and r respectively): ";
    std::cin >> n >> r;
    unsigned long long int a = factorial(n);
    std::cout << "n! = " << a << std::endl;
    unsigned long long int b = factorial(r);
    std::cout << "r! = " << b << std::endl;
    unsigned long long int c = factorial(n - r);
    std::cout << "(n-r)! = " << c << std::endl;
    std::cout << "r!(n-r)! = " << b*c << std::endl;
    value = a / (b*c);
    std::cout << "The value of nCr is : " << value << std::endl;
    system("pause");
    return 0;
}

Output:

Enter a number whose nCr value is to be calculated (n and r respectively): 30 12
n! = 9682165104862298112
r! = 479001600
(n-r)! = 6402373705728000
r!(n-r)! = 12940075575627743232
The value of nCr is : 0
Press any key to continue . . .

CodePudding user response:

The main issue with your code is return type of factorial Method it should be same as the return type of "fact".

Second issue with code is that it cannot handle huge number above i.e, max value of unsigned long long int "18,446,744,073,709,551,615"(https://www.tutorialspoint.com/cplusplus/cpp_data_types.htm). So just change the data types of variables fact,a,b,c and factorial method to "long double" which can accomodate 12 bytes of data.

Code below is just modified for tracing purpose... you can skip the line which you don't need. Be careful with data types. Code is modified as per your requirement for huge calculations. Please reply if you have any confusion. And up-vote my answer if it looks right to you.

You can remove std:: from the code if not need by your compiler.

    #include <iostream>
    long double factorial(int num){       
    long double fact=1;
    for (int i = num; i >=1; i--)
    {
      fact=fact*i;
    }
     return fact;
    }

    int main()
    {
     unsigned long long int n=0,r=0;
     long double value=0;
     std::cout<<"Enter a number whose nCr value is to be calculated (n and r respectively): ";
     std::cin>>n>>r;
     std::cout<<n;
     std::cout<<r;
     long double a=factorial(n);
     long double b=factorial(r);
     long double c=factorial(n-r);
     std::cout<<"\na="<<a;
     std::cout<<"\nb="<<b;
     std::cout<<"\nc="<<c;
     long double d = b*c;
     std::cout<<"\nd="<<d;
     value=(unsigned long long int)(a/d);
     std::cout<<"\nThe value of nCr is : "<<value;
     return 0;
    `enter code here`}
  •  Tags:  
  • c
  • Related