So I've been trying to get big factorials like 50 using BigInteger. But when i run this program in cmd, i get unexpected error at line 16: fact=fact.multiply(new BigInteger(getFactorial(n-1))). I am not even able to see the thrown exception as it gets scrolled out. Please help me find the error.
import java.math.BigInteger;
class FactorialUsingRecursion {
public static void main(String args[]) {
int num=50;
String fact=getFactorial(num);
System.out.println(num+"! = "+fact);
}
static String getFactorial(int n) {
BigInteger fact=new BigInteger(n+"");
if(fact.toString()=="1") {
return "1";
}
fact=fact.multiply(new BigInteger(getFactorial(n-1)));//error line
return fact.toString();
}
}
The problem is with String comparison in if condition. Change that if condition to the following:
if(fact.intValue() == 1) {