java.math.BigInteger.divideAndRemainder(BigInteger val) was introduced in Java 8. This method returns an array of two BigInteger after applying division operation between the BigInteger calling this method and the BigInteger passed as a parameter to the other method. Here, First BigInteger of the array represents a result of the division (this / val) and Second BigInteger Represents remainder of this division operation (this % val).
public BigInteger divideAndRemainder(BigInteger val)
Parameters: This method takes a mandatory parameter val of type BigInteger, which acts as the divisor to apply division operation.
Return Value: This method returns an array of two BigInteger containing the quotient (this / val) as the first element, and the remainder (this % val) as the second element.
Exception: The method throws ArithmeticException if null or 0 is passed in parameter.
Input: 42245, 23 Output: [1836, 17] Input: 25556, 444 Output: [57, 248] Explanation: In input two BigInteger are given. The first one is the dividend, which calls the method, and the second is the divisor, which is passed as parameter. When applying division operation, as a result, an array of quotient and remainder is returned as output. These are also of BigInteger type
Below programs illustrates divideAndRemainder() method of BigInteger class:
Dividend : 25556 Divisor : 444 Quotient : 57 Remainder : 248
Dividend : 32345678987 Divisor : 1537862842 Quotient : 21 Remainder : 50559305
Program 3: To demonstrate ArithmeticException
Dividend : 32345678987 Divisor : 0 Exception: 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.