Couldn't get the error in my code

For the problem named ‘Small Factorials’ my solution is giving correct results every time but on submitting the code it shows wrong answer. Please Help.

#include <bits/stdc++.h>
using namespace std;

int factorial (int a)
{
int x=1;
while (a>=1)
{
x *= a;
a–;
}
return x;
}

int main()
{
int n;
cin>>n;
int arr[n];

if (n>=1 && n<=100)
{
    for (int i=0; i<n; i++)
    {
        int a;
        cin>>a;
        if(a>=1 && a<=100) {arr[i] = factorial(a);}
        else {arr[i] = a;}
    }

    for (auto i : arr)
        cout<<i<<endl;
}
return 0;

}

1 Like

The Constraints mentioned were 1<=n<=100. You need to understand where it goes wrong now.
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Factorial(100) is greater than pow(2,524). So, Not even Long Long int can hold the value of 100!.
You’ll have to use Either Big Integer (available in Java and Python) or concept mentioned here.

I recommend you to use Python since it is extremely easy to handle huge values.
As the python solution for the problem would be:

from math import factorial as fact
for test in range(int(input())):
    print(fact(int(input())))

Happy Coding.:slight_smile:

1 Like

Can you print 100! in python directly by multiplying it by all no. Lest than equal to 100. I never knew that python has such a huge value for int
P.S. : I am talking about python 3.7

It internally handles how the number is stored if it gets large. There was a CC Discuss thread on this, I’m not able to find it though. However remember that operations on such large numbers takes a LOT of time.

Congratulation @aneee004 for becoming CM😉

1 Like

Yes, Implicit conversions take place whenever the value of integer becomes huge.

Thanks! The contest was really great, except for what the hell was the last problem. I saw the unofficial stands and @galencolin had not solved that yet. So I gave up and closed it xD.

I still don’t know why my code is wrong :frowning:

1 Like