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
BigIntegerclass 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
BigIntegerclass.
import java.math.BigInteger;
- In lines 5 and 6: We create two
BigIntegerobjects,val1with value33297360andval2with value676543.
BigInteger val1 = new BigInteger("33297360");
BigInteger val2 = new BigInteger("676543");
- In line 7: We call the
remaindermethod on theval1object withval2as an argument. This method call will return aBigIntegerthat has a value equal to the remainder ofval1 / val2(i.e. 33297360 % 676543 = 146753).
BigInteger result = val1.remainder(val2); //146753