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
- BigDecimal divideAndRemainder() Method in Java with Examples
- BigInteger add() Method in Java with Examples
- BigInteger gcd() Method in Java with Examples
- BigInteger multiply() Method in Java with Examples
- BigInteger isProbablePrime() Method in Java with Examples
- Java 8 | BigInteger byteValueExact() method with Examples
- BigInteger subtract() Method in Java with Examples
- BigInteger sqrtAndRemainder() Method in Java with Examples
- Java 8 | BigInteger shortValueExact() Method with Examples
- BigInteger intValueExact() Method in Java with Examples
- BigInteger divide() Method in Java with Examples
- Java 8 | BigInteger longValueExact() Method with Examples
- BigInteger nextProbablePrime() Method in Java with Examples
- BigInteger xor() Method in Java
- BigInteger mod() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.