My code is showing 'wrong output' in SPOJ, although it's running without trouble in my compiler.
The code for the program is :
#include<stdio.h>
int factorial(int);
int main(){
int a[100],t,n,i;
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&a[i]);
}
for(i=0;i<t;i++){
printf("%d",factorial(a[i]));
printf("\n");
}
return 0;
}
int factorial(int n){
if(n==0){
return 1;
}
else if(n==1){
return 1;
}
else{
return n*factorial(n-1);
}
}
Your program is getting integer overflow. You need at least 66 bytes to store 100! exactly. An unsigned long long int
is usually 8 bytes, and can store up to 1.8 × 1019. 100! is about 9.3 × 10157.
You need another way to calculate this value, or use a different language. You could try storing the values in a double
or long double
, but it isn't going to be exact, so I doubt it will satisfy SPOJ.