# BigDecimal pow() method in Java with Examples

The java.math.BigDecimal.pow(int n) method is used to calculate a BigDecimal raise to the power of some other positive number passed as parameter (this)n. This method performs raises this BigDecimal number to the power of the positive number passed as the parameter.

Syntax:

```public BigDecimal pow(int n)
```

Parameters: This method accepts a parameter n which is the exponent to which we want to raise the power of BigDecimal number.

Returns: This method returns a BigDecimal which is equal to (this)n.

Exception: The parameter n must be in the range 0 through 999999999, inclusive otherwise ArithmeticException is thrown

Below programs illustrate the pow() method of BigDecimal class

Example 1:

 `// Java program to demonstrate ` `// pow() method of BigDecimal ` ` `  `import` `java.math.BigDecimal; ` ` `  `public` `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Creating BigDecimal object ` `        ``BigDecimal b1 ` `            ``= ``new` `BigDecimal(``"924567"``); ` ` `  `        ``// Exponent or power ` `        ``int` `n = ``5``; ` ` `  `        ``// Using pow() method ` `        ``BigDecimal result = b1.pow(n); ` ` `  `        ``// Display result ` `        ``System.out.println(``"Result of pow operation "` `                           ``+ ``"between BigDecimal "` `                           ``+ b1 + ``" and exponent "` `                           ``+ n + ``" equal to "` `                           ``+ result); ` `    ``} ` `} `

Output:

Result of pow operation between BigDecimal 924567 and exponent 5 equal to 675603579456764176151564447607

Example 2:

 `// Java program to demonstrate ` `// pow() method of BigDecimal ` ` `  `import` `java.math.BigDecimal; ` ` `  `public` `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Creating BigDecimal object ` `        ``BigDecimal b1 ` `            ``= ``new` `BigDecimal(``"457863"``); ` ` `  `        ``// Exponent or power ` `        ``int` `n = ``4``; ` ` `  `        ``// Using pow() method ` `        ``BigDecimal result = b1.pow(n); ` ` `  `        ``// Display result ` `        ``System.out.println(``"Result of pow operation "` `                           ``+ ``"between BigDecimal "` `                           ``+ b1 + ``" and exponent "` `                           ``+ n + ``" equal to "` `                           ``+ result); ` `    ``} ` `} `

Output:

Result of pow operation between BigDecimal 457863 and exponent 4 equal to 43948311905876729579361

Example 3: Program showing exception when exponent passed as parameter is less than zero.

 `// Java program to demonstrate ` `// pow() method of BigDecimal ` ` `  `import` `java.math.BigDecimal; ` ` `  `public` `class` `GFG { ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``// Creating BigDecimal object ` `        ``BigDecimal b1 ` `            ``= ``new` `BigDecimal(``"10000"``); ` ` `  `        ``// Negative power ` `        ``int` `n = -``1``; ` ` `  `        ``try` `{ ` `            ``// Using pow() method ` `            ``BigDecimal result = b1.pow(n); ` ` `  `            ``// Display result ` `            ``System.out.println(``"Result of pow operation "` `                               ``+ ``"between BigDecimal "` `                               ``+ b1 + ``" and exponent "` `                               ``+ n + ``" equal to "` `                               ``+ result); ` `        ``} ` `        ``catch` `(Exception e) { ` `            ``System.out.println(e); ` `        ``} ` `    ``} ` `} `

Output:

java.lang.ArithmeticException: Invalid operation

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.