Small Factorial! Wrong Answer!

I have wrote the following code, it works perfectly in my compiler but gives wrong answer on my submission;

#include <iostream>

using namespace std;
int my_func(int n)
{
    int fact;
    if(n==0)
        fact=1;
    if(n==1)
        fact=1;
    else if(n>1)
      fact=n*my_func(n-1);
      return fact;
}
int main()
{
    int i;
    cin >>i;
    for(int x=0;x<i;x++)
    {
        int n;
        cin >>n;
        cout<<my_func(n)<<endl;
    }
    return 0;
}

Could anyone help me out!
Thanks!

100 factorial cannot be stored in any primitive data type in C and C++ . Check the constraints on n

Could you help me out to solve the problem recursively ?

In general, Its a humble request that please do check the problem you are facing on discussion forum search box…!

Whole discussion is present up here in this link: http://discuss.codechef.com/questions/59732/fctrl2codechef-says-giving-a-wrong-answer-while-the-program-is-perfectly-fineplease-help

As @acodebreaker2 100 factorial do not fits in any integer datatype since length of 100 factorial is of 158 numbers.

Go through with the above link you’ll get the detailed explanation up there… :slight_smile:

EDIT: And do check this link of how to compute large factorials in c or c++

discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial

As you are using C++ you don’t have any default mechanisms to store large numbers as large as 100!(158 digits).
You can try using arrays by storing each digit of the number in a different index and modify the printing accordingly. Try to solve the problem using this approach :slight_smile: If you still don’t get it you can always look other’s solution