Java BigInteger Class | remainder() Method with Example

BigInteger Class remainder() method: Here, we are going to learn about the remainder() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 11, 2020

BigInteger Class remainder() method

  • remainder() method is available in java.math package.
  • remainder() method is used to calculate the remainder by using this BigInteger value is to be divided by the given BigInteger value.
  • remainder() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • remainder() method may throw an exception at the time of calculating the remainder.
    ArithmeticException: This exception may throw when the given parameter value is 0.

Syntax:

    public BigInteger remainder(BigInteger val);

Parameter(s):

  • BigInteger val – represents the value by which this BigInteger is to be divided.

Return value:

The return type of this method is BigInteger, it returns BigInteger that holds the remainder by using (this BigInteger) % (BigInteger val).

Example:

// Java program to demonstrate the example 
// of BigInteger remainder(BigInteger val)
// method of BigInteger

import java.math.*;

public class RemainderOfBI {
    public static void main(String args[]) {
        // Initialize two variables divi and divisor
        String divi = "120";
        String divisor = "7";

        // Initialize two BigInteger objects 
        BigInteger b_int1 = new BigInteger(divi);
        BigInteger b_int2 = new BigInteger(divisor);

        // Display b_int1 and b_int2
        System.out.println("b_int1: " + b_int1);
        System.out.println("b_int2: " + b_int2);

        System.out.println("remainder(BigInteger): ");

        // Here, this method remainder(BigInteger) 
        // is used to divide this BigInteger (b_int1) by the
        // given BigInteger (b_int2) and return the BigInteger
        // object that holds the value of the remainder
        BigInteger remainder = b_int1.remainder(b_int2);
        System.out.println("b_int1.remainder(b_int2): " + remainder);
    }
}

Output

b_int1: 120
b_int2: 7
remainder(BigInteger): 
b_int1.remainder(b_int2): 1



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.