ANteelauh Farnese - 8 months ago 27

Java Question

I've been learning some Java and I need help with the following:

I want to divide a BigInteger by a regular integer (i.e. int) but I don't know how to do this. I did a quick search on Google and on Stack Exchange but didn't find any answers.

So, how can I divide a BigInteger by an int? And while we're at it, how can I add/subtract BigInts to ints, compare BigInts to ints, et cetera? Thanks in advance for any helpful help I get.

Answer

Convert the `Integer`

to `BigInteger`

and than divide both `BigInteger`

, as following:

```
BigInteger b = BigInteger.valueOf(10);
int x = 6;
//convert the integer to BigInteger.
BigInteger converted = new BigInteger(Integer.toString(x));
//now you can divide, add, subtract etc.
BigInteger result = b.divide(converted); //but this will give you Integer values.
System.out.println(result);
result = b.add(converted);
System.out.println(result);
```

Above division will give you `Integer`

values of divisions, to get exact values, use `BigDecimal`

.