BigInteger divide() Method in Java with Examples

The java.math.BigInteger.divide(BigInteger val) is used to calculate the division of two BigIntegers. BigInteger class internally uses array of integers for processing, the operation on object of BigIntegers are not as fast as on primitives. This method performs an operation upon the current BigInteger by which this method is called and BigInteger passed as the parameter.

Syntax:

public BigInteger divide(BigInteger val)

Parameters: This method accepts a parameter val which is the value that divides this BigInteger.

Return value: This method returns a BigInteger which holds division (this / val) in Integer (non – floating point value) i.e. it rounds off the result to its floor value.

Exception: The parameter val must not be 0 otherwise Arithmetic Exception is thrown.

Below programs is used to illustrate the divide() method of BigInteger.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// divide() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigInteger object to store result
        BigInteger div;
  
        // Two objects of String created
        // Holds the values to calculate the division
        String input1 = "400000000000000000"
                        + "000000000000000000";
  
        String input2 = "8000000";
  
        // Convert the string input to BigInteger
        BigInteger a
            = new BigInteger(input1);
        BigInteger b
            = new BigInteger(input2);
  
        // Using divide() method
        div = a.divide(b);
  
        // Display the result in BigInteger
        System.out.println("The division of\n"
                           + a + " \nby\n" + b + " "
                           + "\nis\n" + div);
    }
}

chevron_right


Output:

The division of
400000000000000000000000000000000000 
by
8000000 
is
50000000000000000000000000000

Example 2: To demonstrate how it rounds off the result

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// divide() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigInteger object to store result
        BigInteger div;
  
        // Two objects of String created
        // Holds the values to calculate the division
        String input1 = "456216545";
  
        String input2 = "21255132";
  
        // Convert the string input to BigInteger
        BigInteger a
            = new BigInteger(input1);
        BigInteger b
            = new BigInteger(input2);
  
        // Using divide() method
        div = a.divide(b);
  
        // Display the result in BigInteger
        System.out.println("The division of\n"
                           + a + " \nby\n" + b + " "
                           + "\nis " + div);
  
        double d = Double.parseDouble(input1)
                   / Double.parseDouble(input2);
  
        // Display result in double type
        // To match both the results
        System.out.print("Using double result is " + d);
    }
}

chevron_right


Output:

The division of
456216545 
by
21255132 
is 21
Using double result is 21.46383024109189

Example 3: To demonstrate Exception thrown when divided by 0

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// divide() method of BigInteger
  
import java.math.BigInteger;
  
public class GFG {
    public static void main(String[] args)
    {
        // BigInteger object to store result
        BigInteger div;
  
        // Two objects of String created
        // Holds the values to calculate the division
        String input1 = "456216545"
                        + "452133155";
  
        String input2 = "0";
  
        // Convert the string input to BigInteger
        BigInteger a
            = new BigInteger(input1);
        BigInteger b
            = new BigInteger(input2);
  
        // Using divide() method
        try {
            div = a.divide(b);
  
            // Display the result in BigInteger
            System.out.println("The division of\n"
                               + a + " \nby\n" + b + " "
                               + "\nis\n" + div + "\n");
        }
        catch (ArithmeticException e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

java.lang.ArithmeticException: BigInteger divide by zero

Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#divide(java.math.BigInteger)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Akanksha_Rai