VOOZH about

URL: https://www.geeksforgeeks.org/java/java-8-biginteger-divideandremainder-method-with-examples/

⇱ Java 8 | BigInteger divideAndRemainder() method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java 8 | BigInteger divideAndRemainder() method with Examples

Last Updated : 4 Dec, 2018
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). Syntax:
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. Example:
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: Program 1:
Output:
Dividend : 25556
Divisor : 444
Quotient : 57
Remainder : 248
Program 2:
Output:
Dividend : 32345678987
Divisor : 1537862842
Quotient : 21
Remainder : 50559305
Program 3: To demonstrate ArithmeticException
Output:
Dividend : 32345678987
Divisor : 0
Exception: java.lang.ArithmeticException: BigInteger divide by zero
Reference: https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#divideAndRemainder-java.math.BigInteger-
Comment