i am getting output but it shows wrong answer
/* package codechef; // don’t place package name! */
import java.util.;
import java.lang.;
import java.io.*;
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t,n;
t=sc.nextInt();
for(int i=0;i<t;i++)
{ n=sc.nextInt();
int ans=n;
if(n>0) {
for(int j=1;j<n;j++)
{ans=ans*j;
}System.out.println(ans);}
else {System.out.println(“1”);}
}
}}
If I am right you want to print factorial of a number.
Try storing previous values to calculate factorial,
like the following code snippet:
int fact[n+1];
fact[0]=1;
for(int i=1;i<=n;++i){
fact[i]=fact[i-1]*i;
}
cout<<fact[n]<<endl // print fact[nth number]
Time complexity: O(n)
I think you can convert this in java , and check for size overflows based on the problem.
actually i used int which doesn’t have enough range to store the big no. decimal, thanks btw
Well in case of that, ( if time is not the matter) then you can use strings to multiply. Give this a read: Find the Factorial of a large number - GeeksforGeeks