-1

I'm trying to calculate and display factorial of number 100, but this code gives 0 for higher numbers.

int fact(int n){
int res=1;
for(int i=2; i<=n; i++){
    res=res*i;
}
return res;

}

  • 2
    100! is much too big for `int`. Why do you need it? For an approximation, use `double`. For an exact value, you need extended-precision software. – Eric Postpischil May 21 '21 at 03:21
  • 2
    100! is an *enormous* number, with 157 digits. You will never store it in an `int`, or any other native type. There is a YouTube video entitled something like 'ask Alexa 67!'. The device computes it OK and recites it. The video goes on, and on, and on ... – user207421 May 21 '21 at 03:35

4 Answers4

2

The factorial of 100 is 9.332622e+157 can be stored in array and display the array elements. You can go through this link - https://www.geeksforgeeks.org/factorial-large-number/

#define MAX 200
void factorial(int n){
    int res[MAX];

    res[0] = 1;
    int res_size = 1;

    // Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n
    for (int x=2; x<=n; x++)
        res_size = multiply(x, res, res_size);

    cout << "Factorial of given number is \n";
    for (int i=res_size-1; i>=0; i--)
        cout << res[i];
    }
}

// This function multiplies x with the number
// represented by res[].
// res_size is size of res[] or number of digits in the
// number represented by res[]. This function uses simple
// school mathematics for multiplication.
// This function may value of res_size and returns the
// new value of res_size
int multiply(int x, int res[], int res_size)
{
    int carry = 0;  // Initialize carry

    // One by one multiply n with individual digits of res[]
    for (int i=0; i<res_size; i++)
    {
        int prod = res[i] * x + carry;

        // Store last digit of 'prod' in res[] 
        res[i] = prod % 10; 

        // Put rest in carry
        carry  = prod/10;   
    }

    // Put carry in res and increase result size
    while (carry)
    {
        res[res_size] = carry%10;
        carry = carry/10;
        res_size++;
    }
    return res_size;
}
2

As mentioned in the comments, the value of 100! is too large to fit in a native integer type.

One solution is to use a library such as boost::multiprecision.

Here is a sample program, using your code, but instead of int the boost::multiprecision::cpp_int is used:

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>

using Int = boost::multiprecision::cpp_int;
 
Int fact(int n)
{
  Int res = 1;
  for(int i=2;  i<= n; i++)
    res = res * i;
  return res; 
}

int main() 
{
    std::cout << fact(100);
}

Output:

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Here is a live example of the usage, including other factorial values.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0

100! is too large to store in an Int.

In C++, the size of int is the size of a word on the machine architecture.

In a 32-bit architecture, the word size is 32-bits.

The largest number you can represent using a signed 32-bit int is 2,147,483,647, which is much much much smaller than 100!.

Even if you used an unsigned 64-bit integer (uint64_t), 100! would be too large to fit.

Alan
  • 45,915
  • 17
  • 113
  • 134
-2

the max int is 2147483647, I think the number is overflow. I tried,this function is working well below the para is 17,after that,the result is more than 2147483647. You can google how the computer store the positive number,negative number and so on.It must be more clear.

Arvin
  • 1
  • 1