#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