2

I am trying to run this algorithm in c++ in order to get a big number

#include<iostream>

using namespace std;
int main()
{
    int num,factorial=1;
    cout<<" Enter Number To Find Its Factorial:  ";

    cin>>num;

    for(int a=1;a<=num;a++)
    {
       factorial=factorial*a;
    }

    cout<<"Factorial of Given Number is ="<<factorial<<endl;
    return 0;
}

How can I declare a Big Integer like in Java instead of an int?

Kevin
  • 2,743
  • 3
  • 18
  • 28

4 Answers4

2

There is no big-integer support in the C++ standard library. A common choice for big-number arithmetic is GMP. After downloading and installing the library, in your code you would #include <gmpxx.h> and declare mpz_class factorial instead of int factorial, then link against GMP.

Linking with GMP can be done in an IDE, by adding GMP in your editor’s compile settings; or by adding -lgmp to your compilation command (e.g., g++ or clang++).

Jon Purdy
  • 51,678
  • 7
  • 93
  • 161
0

There is no arbitrary-precision arithmetic in c++ standard library. You'll need to implement it yourself using an array of integers, or use an existing non-standard library.

eerorika
  • 223,800
  • 12
  • 181
  • 301
0

Try unsigned long long int for just convenience.

I just want to leave it as comment but I have low repu for commenting..

Chickenchaser
  • 174
  • 2
  • 13
0

There is no standard support for arbitrary-precision integers. However, a few libraries are available for handling big integers:

Mathieu Rodic
  • 6,459
  • 2
  • 41
  • 49