# what is wrong in this code small factorial

#include<stdio.h>
int main()
{
int t,arr[200],i,j,num,temp,x,m;

``````scanf("%d",&t);
while(t--)
{
scanf("%d",&num);
m=0;
temp=0;

while(num!=0)
{
arr[m]=num%10;
num=num/10;
m++;
}
for(i=num-1;i>=1;i--)
{
for(j=0;j<m;j++)
{
x=arr[j]*i +temp;
arr[j]=x%10;
temp=x/10;

}
while(temp>0)
{
arr[m]=temp%10;
temp=temp/10;
m++;
}

}
for(i=m-1;i>=0;i--)
{
printf("%d",arr[i]);
}
printf("\n");
}
return 0;
``````

}

Your implementation is wrong. Check for any small input lets say, 4 , it doesn’t return 24.

This is the correct implementation as per this editorial:

``````cin>>n;
int a[202],i;
for(i=0;i<=201;i++)
a[i]=0;

int j,carry=0,digits=1,middle;
a[0]=1;
for(i=1;i<=n;i++)
{
for(j=0;j<digits;j++)       // multiplying the current i with the factorial of (i-1) already computed
{                           // and stored inside the array
middle=a[j]*i +carry;
a[j]=middle%10;
carry=middle/10;
}
while(carry>0)         // if carry left, then we are incrementing the digits.
{
a[digits]=carry%10;
carry/=10;
digits++;
}
}

for(i=digits-1; i>=0;i--)
cout<<a[i];
``````

PS: I assumed you went through the blog as your implementation seems to be based on that idea but not correct!! If not, just go through it once and then you may ask questions or doubts here…