what's wrong with the following small factorial solution?

#include<stdio.h>
int main()
{
int i,j,t,x,fact=1;
scanf("%d",&t);
for(i=0;i<t;i++)
{
scanf("%d",&x);
for(j=1;j<=x;j++)
{
fact=fact*j;
}
printf("%d\n",fact);
}
return 0;
}

@sameerkhan005 : You can’t store factorials of numbers upto 100 in “integer” or “long” or “long long” .
You need to implement some sort of “BigInteger” class for this problem .

1 Like

@sameerkhan005: you have to initialize fact to 1 in the outer ‘for’ loop. In your code when you take second input for calculating the factorial, previous result is already in the ‘fact’ variable. To correct this you just have to insert the statement “fact=1;” just before or after “scanf(”%d",&x);".

yes that is another bug . But you wont get “Correct Answer” till you handle overflow

1 Like