small factorials-why is this code a wrong answer?

#include
using namespace std;
int main()
{ int t,j,i;
int n[100];
long double fact;
cin>>t;

  if(t>=1 && t<=100)
  { for(i=1;i<=t;i++)
   { cin>>n*;
   }
   for(i=1;i<=t;i++)
   { fact=1;
     if(n*>=1 && n*<=100)
     { for(j=1;j<=n*;j++)
       { fact= fact*j;
       }
       cout<<fact<<endl;
     }
   }
 }
   return 0;
}

your algorithm is true only up to certain factorial like 19 after that overflow occurs and thus you encounter wrong answer, this is implemented using arrays storing each digit in an array, for eg: 123 can be stored in an array as a[0]=3,a1=2,a[2]=1 and so on… there is a tutorial to solve this problem.
you can look at this tutorial to solve this problem. if you have any doubts after looking at this tutorial comment below. happy coding :slight_smile:

2 Likes

Ohh…got it!! Thanks a lot! :slight_smile:

I would like to suggest… http://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial …this is a very good tutorial written by @kuruma!!!

1 Like