Java BigInteger Class | divide() Method with Example

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

BigInteger Class divide() method

  • divide() method is available in java.math package.
  • divide() method is used to divide this BigInteger by the given BigInteger (divsr) (i.e. this BigInteger / BigInteger).
  • divide() 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.
  • divide() method does not throw an exception at the time of dividing the objects.

Syntax:

    public BigInteger divide(BigInteger divsr);

Parameter(s):

  • BigInteger divsr – represents the divisor by which to divide this object.

Return value:

The return type of this method is BigInteger, it returns BigInteger and its value is calculated by using (this BigInteger)/ (BigInteger divsr).

Example:

// Java program to demonstrate the example 
// of divide(BigInteger divsr) method of BigInteger

import java.math.*;

public class DivideOfBI {
    public static void main(String args[]) {
        // Initialize two variables divi and divisr
        String divi = "100";
        String divisr = "5";

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

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

        // divides this BigInteger b_int1 by the 
        // given BigInteger b_int2 like b_int1/b_int2
        BigInteger divide_val = b_int1.divide(b_int2);
        System.out.println("b_int1.divide(b_int2): " + divide_val);
    }
}

Output

b_int1: 100
b_int2: 5
b_int1.divide(b_int2): 20


Comments and Discussions!

Load comments ↻





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