A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-8-biginteger-divideandremainder-method-with-examples/ below:

Java 8 | BigInteger divideAndRemainder() method with Examples

Java 8 | BigInteger divideAndRemainder() method with Examples

Last Updated : 04 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: Java
// 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
// 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
// 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-

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4