# Small Factorial

//my code for smallfactorial,it’s giving correct output in my system but not here…please tell me whats wrong in it???

``````#include <stdio.h>
#include <stdlib.h>
int fact(int);

int main()
{
int t,n,r;
scanf("%d",&t);
while(t--)
{
r=0;
scanf("%d",&n);
r=fact(n);
printf("%d\n",r);
}
return 0;
}
int fact(int n)
{

if(n==1)
return 1;
else
return(n*fact(n-1));
}``````

it will not work for larger inputs…by larger i mean nos like 20-30 will also give a WA…as integer will overflow!!!

try putting 100…the worst case!!!

@kunal361- thanks for your ans,u r ri8 but i used unsigned long integer variable for storing the result but it is still not working…

shivali24: Just think about it for a while. What’s the range of int? Approximately 10^9. What’s the range of long long? Approximately 10^18. How large can the result get? Try a calculator: 30! is around 10^32. Well… shit.

Of course it won’t work with any integer type of C/C++. Try using "bigint"s - represent a number as an array of base-10 digits. (It works a bit faster if you use a larger base.) You just need to implement the addition and multiplication of such numbers, which is easy enough in O(number of digits ^2) in the same way as you’d do it on paper.