Luffy Singh - 6 months ago 54

Java Question

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 ?

Answer

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 + "")`

Source (Stackoverflow)