# BigDecimal divideAndRemainder() Method in Java with Examples

The java.math.BigDecimal.divideAndRemainder(BigDecimal divisor) is used to calculate both quotient and remainder of two BigDecimals. If both the integer quotient and remainder are needed then this method is faster than using the divideToIntegralValue() and remainder() methods separately because the division need only be carried out once. This method performs an operation upon the current BigDecimal by which this method is called and the BigDecimal passed as the parameter.

There are two overloads of divideAndRemainder method available in Java which is listed below:

• divideAndRemainder(BigDecimal divisor)
• divideAndRemainder(BigDecimal divisor, MathContext mc)

### divideAndRemainder(BigDecimal divisor)

Syntax:

```public BigDecimal[] divideAndRemainder(BigDecimal divisor)
```

Parameters: This method accepts a parameter divisor by which this BigDecimal is to be divided for obtaining remainder and quotient.
Return value: This method returns a BigDecimal array of size two, which holds the quotient and remainder.
Exception: The parameter divisor must not be 0 otherwise Arithmetic Exception is thrown.

Below programs is used to illustrate the divideAndRemainder() method of BigDecimal.

 `// Java program to demonstrate ` `// divideAndRemainder() method of BigDecimal ` ` `  `import` `java.math.*; ` ` `  `public` `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// BigDecimal object to store the result ` `        ``BigDecimal res[]; ` ` `  `        ``// For user input ` `        ``// Use Scanner or BufferedReader ` ` `  `        ``// Two objects of String created ` `        ``// Holds the values ` `        ``String input1 ` `            ``= ``"456865265569"``; ` `        ``String input2 ` `            ``= ``"65245"``; ` ` `  `        ``// Convert the string input to BigDecimal ` `        ``BigDecimal a ` `            ``= ``new` `BigDecimal(input1); ` `        ``BigDecimal divisor ` `            ``= ``new` `BigDecimal(input2); ` ` `  `        ``// Using divideAndRemainder() method ` `        ``res = a.divideAndRemainder(divisor); ` ` `  `        ``// Display the result in BigDecimal ` `        ``System.out.println(``"Quotient = "` `+ res[``0``] ` `                           ``+ ``"\nRemainder = "` `+ res[``1``]); ` `    ``} ` `} `

Output:

```Quotient = 7002303
Remainder = 6334
```

### divideAndRemainder(BigDecimal divisor, MathContext mc)

This method is used to calculate the quotient which is the result of divideToIntegralValue() followed by the result of remainder() on the two operands calculated with rounding according to the context settings.

Syntax:

```public BigDecimal[] divideAndRemainder(BigDecimal divisor,
MathContext mc)
```

Parameters: This method accepts two parameters:

• divisor by which this BigDecimal is to be divided
• mc of type MathContext for context settings.

Return value: This method returns a BigDecimal array of size two, which holds the quotient and remainder.

Exception: The method throws Arithmetic Exception for following conditions:

• If the parameter divisor is 0.
• If the result is inexact but the rounding mode is UNNECESSARY or mc.precision > 0 and the result of this.divideToIntgralValue(divisor) would require a precision of more than mc.precision digits.

Below programs is used to illustrate the divideAndRemainder() method of BigDecimal.
Program 1:

 `// Java program to demonstrate ` `// divideAndRemainder() method of BigDecimal ` ` `  `import` `java.math.*; ` ` `  `public` `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// BigDecimal object to store the result ` `        ``BigDecimal res[]; ` ` `  `        ``// For user input ` `        ``// Use Scanner or BufferedReader ` ` `  `        ``// Two objects of String created ` `        ``// Holds the values ` `        ``String input1 ` `            ``= ``"4568652655"``; ` `        ``String input2 ` `            ``= ``"2562"``; ` ` `  `        ``// Convert the string input to BigDecimal ` `        ``BigDecimal a ` `            ``= ``new` `BigDecimal(input1); ` `        ``BigDecimal divisor ` `            ``= ``new` `BigDecimal(input2); ` ` `  `        ``// Set precision to 10 ` `        ``MathContext mc ` `            ``= ``new` `MathContext(``10``); ` ` `  `        ``try` `{ ` `            ``// Using divideAndRemainder() method ` `            ``res = a.divideAndRemainder(divisor, mc); ` ` `  `            ``// Display the result in BigDecimal ` `            ``System.out.println(``"Quotient = "` `+ res[``0``] ` `                               ``+ ``"\nRemainder = "` `+ res[``1``]); ` `        ``} ` `        ``catch` `(Exception e) { ` ` `  `            ``System.out.println(e); ` `        ``} ` `    ``} ` `} `

Output:

```Quotient = 1783236
Remainder = 2023
```

Program 2: Program showing exception thrown by method divideAndRemainder().

 `// Java program to demonstrate ` `// divideAndRemainder() method of BigDecimal ` ` `  `import` `java.math.*; ` ` `  `public` `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// BigDecimal object to store the result ` `        ``BigDecimal res[]; ` ` `  `        ``// For user input ` `        ``// Use Scanner or BufferedReader ` ` `  `        ``// Two objects of String created ` `        ``// Holds the values ` `        ``String input1 ` `            ``= ``"4568652655"``; ` `        ``String input2 ` `            ``= ``"2562"``; ` ` `  `        ``// Convert the string input to BigDecimal ` `        ``BigDecimal a ` `            ``= ``new` `BigDecimal(input1); ` `        ``BigDecimal divisor ` `            ``= ``new` `BigDecimal(input2); ` ` `  `        ``// Set precision to 5 ` `        ``MathContext mc ` `            ``= ``new` `MathContext(``5``); ` ` `  `        ``try` `{ ` `            ``// Using divideAndRemainder() method ` `            ``res = a.divideAndRemainder(divisor, mc); ` ` `  `            ``// Display the result in BigDecimal ` `            ``System.out.println(``"Quotient = "` `+ res[``0``] ` `                               ``+ ``"\nRemainder = "` `                               ``+ res[``1``]); ` `        ``} ` `        ``catch` `(Exception e) { ` ` `  `            ``System.out.println(e); ` `        ``} ` `    ``} ` `} `

Output:

```java.lang.ArithmeticException: Division impossible
```

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.