My code and output is correct but when I submit it says my program is wrong. Here’s my code :
import java.util.Scanner;
class CodeChef
{
public static void main (String[] args)
{
int fact=1;int a;
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t&&t>=1&&t<=100;i++)
{
a=sc.nextInt();
while (a!=0&&a>=1&&a<=100)
{
fact=fact*a;
a--;
}
System.out.println(fact);
fact=1;
}
}
}
Your code is right but you see factorials of a number is very large int can’t store above factorial of 20( that too i doubt) even long long or unsigned 64-bit integer can’t store value of 100!(unsigned 64-bit integer can store value upto 19 digits whereas 100! is 150+ digits)you need to be cleverer here see this has the solution http://iamit.in/blog/Spoj-Problem-Small-Factorials-FCTRL2-Explanation-and-Solution/
Have a look at this. You are doing same mistake-> https://discuss.codechef.com/questions/128558/whats-wrong-with-this-code?page=1#128562
I think you should use Big integer in Java
Ps:- I am not so sure about big int thing