I am trying to do Factorial with Recursion and BigIntegers but eclipse is complaining about the BigInteger. I know the program is supposed to be simple but it is giving me headache. Here is the code.
import java.util.Scanner;
import java.math.BigInteger;
public class Factorial
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter integer");
BigInteger n = input.nextBigInteger();
System.out.println("Factorial of " + n + " is " + fact(n));
}
public static int fact(BigInteger n)
{
if(n ==0)
{
return 1;
}
else
{
return n * fact(n-1);
}
}
}
BigIntegerdoes not support comparison using==and multiplication using*. Instead, you have to call the appropriate methods of theBigIntegerclass (equals()andmultipy()).Also note that there exist
BigInteger.ZEROandBigInteger.ONE.Finally, the return type of your
factmethod should beBigIntegerand notint. Whether you want the argument to be of typeBigIntegerorintis up to you.