java.math.BigInteger divide(BigInteger val)
Description
On this document we will be showing a java example on how to use the divide(BigInteger val) method of BigInteger Class. Basically this method returns a BigInteger whose value is (this / val). Since the result would be BigInteger and wouldn’t be able to hold fractions, then the remainder would be discarded. If you are interested on the remainder better use the divideAndRemainder(BigInteger val) method of BigInteger class.
Notes:
- this method throws an ArithmeticException when the val method argument supplied is zero.
Method Syntax
public BigInteger divide(BigInteger val)
Method Argument
Data Type | Parameter | Description |
---|---|---|
BigInteger | val | value by which this BigInteger is to be divided. |
Method Returns
The divide() method returns this / val
.
Compatibility
Requires Java 1.1 and up
Java BigInteger divide() Example
Below is a java code demonstrates the use of divide() method of BigInteger class. The example presented might be simple however it shows the behavior of the divide() method.
package com.javatutorialhq.java.examples; import java.math.BigInteger; import java.util.Scanner; /* * A java example source code to demonstrate * the use of divide() method of BigInteger class */ public class BigIntegerDivideExample { public static void main(String[] args) { // ask for user input System.out.print("Enter the dividend:"); Scanner s = new Scanner(System.in); String dividend = s.nextLine(); System.out.print("Enter the divisor:"); String divisor = s.nextLine(); s.close(); // convert the string input to BigInteger BigInteger val1 = new BigInteger(dividend); BigInteger val2 = new BigInteger(divisor); // get the quotient BigInteger result = val1.divide(val2); System.out.println("Result of the operation is:"+result); } }
This example is a lot simpler than it looks. We simply ask the user for two inputs, the first one is the dividend and the other is divisor. The quotient were derived in using the divide() method of the BigInteger class.