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,813
  • 3
  • 20
  • 30

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
  • 53,300
  • 8
  • 96
  • 166
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
  • 232,697
  • 12
  • 197
  • 326
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
  • 184
  • 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,637
  • 2
  • 43
  • 49