What is the BigInteger.remainder method in Java?

The remainder method of the BigInteger class can be used to get the remainder of the division of the current BigInteger object by the passed BigInteger.

In other words, the function performs the modulo operation (%) for very big integers.

In Java, the BigInteger class handles very big integer mathematical operations that are outside the limits of all primitive types.

Syntax

public BigInteger remainder(BigInteger val)

Parameters

This method takes a BigInteger object as an argument. remainder throws ArithmeticException if the value of the argument is 0.

Return value

The remainder method returns a BigInteger object. The value of the returned BigInteger object is the remainder of the division of the current BigInteger value by the passed argument value.

Code

The example below demonstrates how to use the remainder method.

import java.math.BigInteger;
class BigIntegerRemainderExample {
public static void main( String args[] ) {
BigInteger val1 = new BigInteger("33297360");
BigInteger val2 = new BigInteger("676543");
BigInteger result = val1.remainder(val2);
System.out.println(result);
}
}

Explanation

In the above code:

  • In line 1: We import the BigInteger class.
import java.math.BigInteger;
  • In lines 5 and 6: We create two BigInteger objects, val1 with value 33297360 and val2 with value 676543.
BigInteger val1 = new BigInteger("33297360");
BigInteger val2 = new BigInteger("676543");
  • In line 7: We call the remainder method on the val1 object with val2 as an argument. This method call will return a BigInteger that has a value equal to the remainder of val1 / val2 (i.e. 33297360 % 676543 = 146753).
BigInteger result = val1.remainder(val2); //146753