BigInteger remainder() Method in Java

The java.math.BigInteger.remainder(BigInteger big) method returns a BigInteger whose value is equal to (this BigInteger % big(BigInteger passed as parameter)).The remainder operation finds the remainder after division of this BigInteger by another BigInteger passed as parameter.

Syntax:

public BigInteger remainder(BigInteger big)

Parameter: The function accepts a single mandatory parameter big which specifies the BigInteger Object by which we want to divide this bigInteger object.



Returns: The method returns the BigInteger which is equal to this BigInteger % big(BigInteger passed as parameter).

ExceptionThe method throws an ArithmeticException – when big = 0

Examples:

Input: BigInteger1=321456, BigInteger2=31711
Output: 4346
Explanation: BigInteger1.remainder(BigInteger2)=4346. The divide operation 
between 321456 and 31711 returns 4346 as remainder.

Input: BigInteger1=59185482345, BigInteger2=59185482345
Output: 0
Explanation: BigInteger1.remainder(BigInteger2)=0. The divide operation between 
59185482345 and 59185482345 returns 0 as remainder.

Example 1: Below programs illustrate remainder() method of BigInteger class

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate remainder() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("321456");
        b2 = new BigInteger("31711");
  
        // apply remainder() method
        BigInteger result = b1.remainder(b2);
  
        // print result
        System.out.println("Result of remainder "
                           + "operation between " + b1
                           + " and " + b2 + " equal to " + result);
    }
}

chevron_right


Output:

Result of remainder operation between 321456 and 31711 equal to 4346

Example 2: when both are equal in value.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate remainder() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("3515219485");
        b2 = new BigInteger("3515219485");
  
        // apply remainder() method
        BigInteger result = b1.remainder(b2);
  
        // print result
        System.out.println("Result of remainder "
                           + "operation between " + b1
                           + " and " + b2 + " equal to " + result);
    }
}

chevron_right


Output:

Result of remainder operation between 3515219485 and 3515219485 equal to 0

Example 3: program showing exception when BigInteger passed as parameter is equal to zero.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate remainder() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // Creating 2 BigInteger objects
        BigInteger b1, b2;
  
        b1 = new BigInteger("3515219485");
        b2 = new BigInteger("0");
  
        // apply remainder() method
        BigInteger result = b1.remainder(b2);
  
        // print result
        System.out.println("Result of remainder "+
                "operation between " + b1
                + " and " + b2 +
                " equal to " + result);
    }
}

chevron_right


Output:
Exception in thread "main" java.lang.ArithmeticException: BigInteger divide by zero
    at java.math.MutableBigInteger.divideKnuth(MutableBigInteger.java:1179)
    at java.math.MutableBigInteger.divideKnuth(MutableBigInteger.java:1163)
    at java.math.BigInteger.remainderKnuth(BigInteger.java:2167)
    at java.math.BigInteger.remainder(BigInteger.java:2155)
    at GFG.main(GFG.java:16)

Reference:
BigInteger remainder() Docs



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.