#include<stdio.h>
int fact(int n)
{
if(n==1)
return 1;
else
return (fact(n-1)*n);
}
main()
{
int f,i,t,a[20];
scanf("%d",&t);
for(i=0;i<t;i++)
scanf("%d",&a[i]);
for(i=0;i<t;i++)
{
f=fact(a[i]);
printf("%d\n",f);
}
return 0;
}
Read the constraints carefully. 1<=t<=100 but you are declaring array size of “a[ ]” as 20. So ArrayIndexOutOfBounds runtime error for t>=20 occurs.
Also your approach for this problem is not correct, 1<=n<=100 so you need to be able to produce the value of upto 100! ( factorial of 100 ) which is a very large number ( won’t fit in long long int).
You are declaring a[] of size 20 for storing the value of every test case, what if the number of test cases is greater than 20(t<=100 as given in the problem constraints). This is giving you the run time error. Also, do you think your recursive implementaion will give you the correct answer for n=100 and kind large values? Just check your code and test it once. You’ll probably get an overflow because the factorial of such large numbers will not fit in any datatype.
You’ll have to go through this post for learning something new.