The java.math.BigInteger.divide(BigInteger val) is used to calculate the division of two BigIntegers. BigInteger class internally uses array of integers for processing, the operation on object of BigIntegers are not as fast as on primitives. This method performs an operation upon the current BigInteger by which this method is called and BigInteger passed as the parameter.
public BigInteger divide(BigInteger val)
Parameters: This method accepts a parameter val which is the value that divides this BigInteger.
Return value: This method returns a BigInteger which holds division (this / val) in Integer (non – floating point value) i.e. it rounds off the result to its floor value.
Exception: The parameter val must not be 0 otherwise Arithmetic Exception is thrown.
Below programs is used to illustrate the divide() method of BigInteger.
The division of 400000000000000000000000000000000000 by 8000000 is 50000000000000000000000000000
Example 2: To demonstrate how it rounds off the result
The division of 456216545 by 21255132 is 21 Using double result is 21.46383024109189
Example 3: To demonstrate Exception thrown when divided by 0
java.lang.ArithmeticException: BigInteger divide by zero
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.