java.math.BigInteger remainder(BigInteger val)


On this document we will be showing a java example on how to use the remainder(BigInteger val) method of BigInteger Class. Basically this method performs the modulo operator between this BigInteger and method argument val simply this % m. This is similar in nature with the mod() method of BigInteger class however in using the remainder, negative results will be returned. Meanwhile in using the mod() operator it will always return a positive value. Depending on the circumstances, you can select on which to use.


  • ArithmeticException will be thrown if val method argument is zero.

Method Syntax

public BigInteger remainder(BigInteger val)

Method Argument

Data Type Parameter Description
BigInteger m the modulus

Method Returns

The remainder(BigInteger val) method returns a BigInteger whose value is this % m.


Requires Java 1.1 and up

Java BigInteger remainder(BigInteger val) Example

Below is a java code demonstrates the use of remainder(BigInteger val) method of BigInteger class. The example presented might be simple however it shows the behavior of the remainder(BigInteger val) method.


import java.math.BigInteger;
import java.util.Scanner;

 * A java example source code to demonstrate
 * the use of remainder() method of BigInteger class

public class BigIntegerModExample {

	public static void main(String[] args) {	
		// ask for two input
		System.out.print("Enter a number:");
		Scanner s = new Scanner(;
		String firstInput = s.nextLine();
		System.out.print("Enter the divisor:");
		String secondInput = s.nextLine();
		// convert the string input to BigInteger
		BigInteger value = new BigInteger(firstInput);
		BigInteger modulo = new BigInteger(secondInput);
		// get the remainder in dividing the first input and 
		// second input
		BigInteger result = value.remainder(modulo);
		System.out.println("The remainder is "+result);		


This example is a lot simpler than it looks. We simply ask the user for two inputs and convert these into BigInteger. We then get the result of the remainder() method of the first input and the second input.

Sample Output

Below is the sample output when you run the above example.

BigInteger remainder() example output