Java Guava | IntMath.checkedPow(int b, int k) method with Examples Last Updated : 01 Feb, 2019 Comments Improve Suggest changes Like Article Like Report 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 // 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 // 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 Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#checkedPow-int-int- Comment More infoAdvertise with us Next Article Java Guava | IntMath.checkedPow(int b, int k) method with Examples S Sahil_Bansall Follow Improve Article Tags : Java java-guava Guava-Functions Guava-IntMath Practice Tags : Java Similar Reads Java Guava | IntMath.checkedAdd(int a, int b) method with Examples The checkedAdd(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their sum. Syntax: public static int checkedAdd(int a, int b) Parameters: The method accepts two int values a and b and computes their sum. Return Value: The method returns the sum of 2 min read Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples The checkedMultiply(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their product. Syntax: public static int checkedMultiply(int a, int b) Parameters: The method accepts two int values a and b and computes their product. Return Value: The method r 2 min read Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples The checkedSubtract(int a, int b) is a method of Guava's IntMath Class which accepts two parameters a and b, and returns their difference. Syntax: public static int checkedSubtract(int a, int b) Parameters: The method accepts two int values a and b and computes their difference. Return Value: The me 2 min read Java Guava | LongMath.checkedPow(long b, int k) method with Examples checkedPow(long b, long k) is a method of Guava's LongMath Class which accepts two parameters b and k and is used to find the k-th power of b. Syntax: public static long checkedPow(long b, long k) Parameters: The method accepts two parameters, b and k. The parameter b is called base which is raised 2 min read Java Guava | LongMath.checkedMultiply(int a, int b) method with Examples The checkedMultiply(long a, long b) is a method of Guava's LongMath Class which accepts two parameters a and b, and returns their product. Syntax: public static long checkedMultiply(long a, long b) Parameters: The method accepts two long values a and b and computes their product. Return Value: The m 2 min read Java Guava | pow(int b, int k) method of IntMath Class The method pow(int b, int k) of Guava's IntMath class returns b to the kth power. Even if the result overflows, it will be equal to BigInteger.valueOf(b).pow(k).intValue(). This implementation runs in O(log k) time. Syntax: public static int pow(int b, int k) Exception: The method pow(int b, int k) 2 min read Java Guava | LongMath.checkedAdd(long a, long b) method with Examples The checkedAdd(long a, long b) is a method of Guava's LongMath Class which accepts two parameters a and b, and returns their sum. Syntax: public static long checkedAdd(long a, long b) Parameters: The method accepts two long values a and b and computes their sum. Return Value: The method returns the 2 min read Java Guava | gcd(int a, int b) method of IntMath Class The method gcd(int a, int b) of Guava's IntMath class returns the greatest common divisor of a, b. Syntax : public static int gcd(int a, int b) Where a and b are integers. Return Value : Greatest common divisor of integers a and b. Exceptions : The method gcd(int a, int b) throws IllegalArgumentExce 2 min read Java Guava | factorial(int n) method of IntMath Class with Examples The factorial(int n) method of Guava's IntMath Class returns the product of the first n positive integers, which is n!. Syntax: public static int factorial(int n) Parameter: The method accepts only one parameter n which is of integer type and is to be used to find the factorial. Return Value: This m 2 min read Java Guava | binomial(int n, int k) of LongMath Class with Examples The binomial(int n, int k) method of Guava's LongMath Class accepts two parameters n and k and calculate the value of the binomial coefficient {n}\choose{k}. If the calculated value overflows the maximum value of a long, then the method returns Long.MAX_VALUE i.e, the maximum value of a long. Syntax 2 min read Like