Java Guava | IntMath.checkedPow(int b, int k) method with Examples

checkedPow(int b, int k) is a method of Guava’s IntMath Class which accepts two parameters b and k and is used to find the k-th power of b.

Syntax:

```public static int checkedPow(int b, int k)
```

Parameters: The method accepts two parameters, b and k. The parameter b is called base which is raised to the k-th power.

Return Value: This method returns the k-th power of b.

Exceptions: The method checkedPow(int b, int k) throws ArithmeticException if the k-th power of b overflows in signed int arithmetic.

Below examples illustrate the implementation of the above method:

Example 1:

 `// Java code to show implementation of ` `// checkedPow(int b, int k) method of ` `// Guava's IntMath class ` ` `  `import` `java.math.RoundingMode; ` `import` `com.google.common.math.IntMath; ` ` `  `class` `GFG { ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `b1 = ``5``; ` `        ``int` `k1 = ``7``; ` ` `  `        ``// Using checkedPow(int b, int k) method ` `        ``// of Guava's IntMath class ` `        ``int` `ans1 = IntMath.checkedPow(b1, k1); ` ` `  `        ``System.out.println(b1 + ``" to the "` `                           ``+ k1 + ``"th power is: "` `                           ``+ ans1); ` ` `  `        ``int` `b2 = ``19``; ` `        ``int` `k2 = ``4``; ` ` `  `        ``// Using checkedPow(int b, int k) method ` `        ``// of Guava's IntMath class ` `        ``int` `ans2 = IntMath.checkedPow(b2, k2); ` ` `  `        ``System.out.println(b2 + ``" to the "` `+ k2 ` `                           ``+ ``"th power is: "` `                           ``+ ans2); ` `    ``} ` `} `

Output:

```5 to the 7th power is: 78125
19 to the 4th power is: 130321
```

Example 2:

 `// Java code to show implementation of ` `// checkedPow(int b, int k) method of ` `// Guava's IntMath class ` ` `  `import` `java.math.RoundingMode; ` `import` `com.google.common.math.IntMath; ` ` `  `class` `GFG { ` ` `  `    ``static` `int` `findPow(``int` `b, ``int` `k) ` `    ``{ ` `        ``try` `{ ` ` `  `            ``// Using checkedPow(int b, int k) method of ` `            ``// Guava's IntMath class ` `            ``// This should raise "ArithmeticException" as ` `            ``// b to the kth power overflows in ` `            ``// signed int arithmetic ` `            ``int` `ans = IntMath.checkedPow(b, k); ` ` `  `            ``// Return the answer ` `            ``return` `ans; ` `        ``} ` `        ``catch` `(Exception e) { ` `            ``System.out.println(e); ` `            ``return` `-``1``; ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `b = ``20``; ` `        ``int` `k = ``25``; ` ` `  `        ``try` `{ ` ` `  `            ``// Function calling ` `            ``findPow(b, k); ` `        ``} ` `        ``catch` `(Exception e) { ` `            ``System.out.println(e); ` `        ``} ` `    ``} ` `} `

Output:

```java.lang.ArithmeticException: overflow
```

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next