Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava 8 | BigInteger divideAndRemainder() method with Examples

Java 8 | BigInteger divideAndRemainder() method with Examples

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:




// Java program to demonstrate divideAndRemainder() method
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating a BigInteger object
        BigInteger dividend = new BigInteger("25556");
        BigInteger divisor = new BigInteger("444");
  
        // applying divideAndRemainder() method
        BigInteger[] answers = dividend.divideAndRemainder(divisor);
  
        // print results
        System.out.println("Dividend : " + dividend);
        System.out.println("Divisor : " + divisor);
        System.out.println("Quotient  : " + answers[0]);
        System.out.println("Remainder : " + answers[1]);
    }
}


Output:

Dividend : 25556
Divisor : 444
Quotient  : 57
Remainder : 248

Program 2:




// Java program to demonstrate divideAndRemainder() method
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating a BigInteger object
        BigInteger dividend = new BigInteger("32345678987");
        BigInteger divisor = new BigInteger("1537862842");
  
        // applying divideAndRemainder() method
        BigInteger[] answers = dividend.divideAndRemainder(divisor);
  
        // print results
        System.out.println("Dividend : " + dividend);
        System.out.println("Divisor : " + divisor);
        System.out.println("Quotient  : " + answers[0]);
        System.out.println("Remainder : " + answers[1]);
    }
}


Output:

Dividend : 32345678987
Divisor : 1537862842
Quotient  : 21
Remainder : 50559305

Program 3: To demonstrate ArithmeticException




// Java program to demonstrate divideAndRemainder() method
  
import java.math.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // Creating a BigInteger object
        BigInteger dividend = new BigInteger("32345678987");
  
        // Creating 0 as divisor
        BigInteger divisor = new BigInteger("0");
  
        // print both numbers
        System.out.println("Dividend : " + dividend);
        System.out.println("Divisor : " + divisor);
  
        try {
            // applying divideAndRemainder() method
            BigInteger[] answers = dividend.divideAndRemainder(divisor);
  
            // print results
            System.out.println("Quotient  : " + answers[0]);
            System.out.println("Remainder : " + answers[1]);
        }
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}


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-

RELATED ARTICLES

Most Popular

Recent Comments