So I'm creating a factorial program using BigInteger class. But I keep getting the above error.
public static BigInteger fact(long n){
BigInteger result = BigInteger.ONE;
for(int i = 1; i <= n; ++i){
result = result.multiply(new BigInteger(i));
}
return result;
}
I already found the fix which is just add an empty string with result.
result = result.multiply(new BigInteger(i + ""))
My question is, why do we have to add that empty string ?
As per oracle docs, BigInteger does not have any constructor that takes int as an argument
Secondly you should use BigInteger.valueOf(i);
instead of new BigInteger(i + "")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With