-2

I am writing a program to calculate the factorial of 100. The code is as below. Notwithstanding, the output is 0 as the answer is too big. Is there any answer to display the exact answer? This is because even unsigned long long is not even able to display the factorial of 100. Thank you.

#include <iostream> 
using namespace std; 
int main() 
{
    int n,i,fact=1; 
    cout << "enter the number "<<endl; 
    cin>>n; 
    for(i=1;i<=n;i++) 
    { 
       fact=fact*i; 
    } 
    cout<<"the factorial is "<<fact<<endl; 
} 
  • 3
    You may also be interested in [big integer](https://stackoverflow.com/questions/12988099/big-numbers-library-in-c) libraries. – Nathan Pierson Feb 20 '21 at 16:05

2 Answers2

3

This is a rather simple task. We can do it like we would do it on a piece of paper. We use a std::vector of digits to hold the number. Because the result will be already too big for an unsigned long long for 22!.

The answer will be exact.

With such an approach the calculation is simple. I do not even know what to explain further.

Please see the code:

#include <iostream>
#include <vector>

int main()
{
    std::cout << "Calculate n!   Enter n (max 10000): ";
    if (unsigned int input{}; (std::cin >> input) && (input <= 10000)) {

        // Here we store the resulting number as single digits
        std::vector<unsigned int> result(3000, 0);  // Magic number. Is big enough for 100000!
        result.back() = 1;                          // Start calculation with 1 (from right to left)

        // Multiply up to the given input value
        for (unsigned int count = 2; count <= input; count++)
        {
            unsigned int sum{}, remainder{};
            unsigned int i = result.size() - 1;     // Calculate from right to left
            while (i > 0)
            {
                // Simple multiplication like on a piece of paper
                sum = result[i] * count + remainder;
                result[i--] = sum % 10;
                remainder = sum / 10;
            }
        }
        // Show output. Supporess leading zeroes
        bool showZeros{ false };
        for (const unsigned int i : result) {
            if ((i != 0) || showZeros) {
                std::cout << i;
                showZeros = true;
            }
        }
    }
    else std::cerr << "\nError: Wrong input.";
}

Developed and tested with Microsoft Visual Studio Community 2019, Version 16.8.2.

Additionally compiled and tested with clang11.0 and gcc10.2

Language: C++17

A M
  • 14,694
  • 5
  • 19
  • 44
0

You can use C++ Boost Library to to manipulate such large numbers.

Here is the code:

    #include <bits/stdc++.h>
    #include <boost/multiprecision/cpp_int.hpp>
    using namespace std;
    using namespace boost::multiprecision;
    cpp_int fact(int);
    int main(){
        cpp_int a=1;
        int n;
        cin>>n;
        cout<<fact(n)<<endl;
    }
    cpp_int fact(int x){
        if(x==1)
        return 1;
        cpp_int temp=1;
        temp= x*fact(x-1);
        return temp;
    }
yeerajan
  • 1
  • 1