ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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
ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.