BigInteger divideAndRemainder() in Java

divideAndRemainder(): This method is available in java.math.BigInteger class of Java.

Syntax:

BigInteger[] java.math.BigInteger.divideAndRemainder(BigInteger val)

This method takes one argument of type BigInteger as its parameter. It returns an array of two BigIntegers containing (this / val)followed by (this % val).

Parameters: One parameter is required for this method.

val: value by which this BigInteger is to be divided, and the remainder computed.

Returns: An array of two BigIntegers: the quotient (this / val) is the initial element, and the remainder (this % val) is the final element.

Throws: ArithmeticException - if val is zero.

For Example:

BigInteger bigInteger = new BigInteger("1234")

BigInteger val = new BigInteger("7")

bigInteger.divideAndRemainder(val) = > It returns {176,2}.

Approach

Java

import java.math.BigInteger;

public class BigIntegerdivideAndRemainder {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");
        BigInteger val = new BigInteger("7");
        BigInteger[] arr = bigInteger.divideAndRemainder(val);

        System.out.println(arr[0] + " " + arr[1]);
    }
}

Output:

176 2

No comments:

Post a Comment