# BigDecimal sqrt() Method in Java with Examples

The java.math.BigDecimal.sqrt(MathContext mc) is an inbuilt function added in Java SE 9 & JDK 9 which returns BigDecimal value of square root of a BigDecimal on which sqrt() method is applied with rounding according to the context settings.

Syntax:

```public BigDecimal sqrt(MathContext mc)
```

Parameters: This method accepts a parameter mc of type MathContext for context settings.

Return Value: This method returns an approximation to the square root of this with rounding according to the context settings.

Exception: The method throws ArithmeticException for following conditions.

• If BigDecimal number is less than zero.
• If an exact result is requested (Precision = 0) and there is no finite decimal expansion of the exact result.
• If the exact result cannot fit in Precision digits.

Note: This method is only available from JDK 9.

Below programs are used to illustrate the sqrt() method of BigDecimal:

Example 1:

 `// Java program to demonstrate sqrt() method ` ` `  `import` `java.math.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// Creating a BigDecimal object ` `        ``BigDecimal a, squareRoot; ` ` `  `        ``a = ``new` `BigDecimal(``"100000000000000000000"``); ` ` `  `        ``// Set precision to 10 ` `        ``MathContext mc ` `            ``= ``new` `MathContext(``10``); ` ` `  `        ``// calculate square root of bigDecimal ` `        ``// using sqrt() method ` `        ``squareRoot = a.sqrt(mc); ` ` `  `        ``// print result ` `        ``System.out.println(``"Square root value of "` `+ a ` `                           ``+ ``" is "` `+ squareRoot); ` `    ``} ` `} `

Output:

```Square root value of 100000000000000000000 is 1.000000000E+10
```

Example 2: Showing Exception thrown by sqrt() method.

 `// Java program to demonstrate sqrt() method ` ` `  `import` `java.math.*; ` ` `  `class` `GFG { ` ` `  `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` ` `  `        ``// Creating a BigDecimal object ` `        ``BigDecimal a, squareRoot; ` ` `  `        ``a = ``new` `BigDecimal(``"-4"``); ` ` `  `        ``// Set precision to 10 ` `        ``MathContext mc ` `            ``= ``new` `MathContext(``10``); ` ` `  `        ``// calculate square root of bigDecimal ` `        ``// using sqrt() method ` `        ``try` `{ ` `            ``squareRoot = a.sqrt(mc); ` ` `  `            ``// print result ` `            ``System.out.println(``"Square root"` `                               ``+ ``" value of "` `+ a ` `                               ``+ ``" is "` `+ squareRoot); ` `        ``} ` `        ``catch` `(Exception e) { ` `            ``System.out.println(e); ` `        ``} ` `    ``} ` `} `

Output:

```java.lang.ArithmeticException: Attempted square root of negative BigDecimal
```

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.