small factorials-why is this code a wrong answer?

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

  if(t>=1 && t<=100)
  { for(i=1;i<=t;i++)
   { cin>>n[i];
   { fact=1;
     if(n[i]>=1 && n[i]<=100)
     { for(j=1;j<=n[i];j++)
       { fact= fact*j;
   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:


I would like to suggest… …this is a very good tutorial written by @kuruma!!!

1 Like

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