Java Guava | factorial(int n) method of IntMath Class with Examples Last Updated : 19 Sep, 2023 Comments Improve Suggest changes Like Article Like Report 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 method return following values: This method returns 1 if n is 0. This method returns product of the first n positive integers if the result fits in a int. This method returns Integer.MAX_VALUE if the result does not fit in a int. Exceptions: The method factorial(int n) throws IllegalArgumentException if n is negative. Example 1: Java // Java code to show implementation of // factorial(int n) 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 n1 = 10; // Using factorial(int n) method of // Guava's IntMath class int ans1 = IntMath.factorial(n1); System.out.println("factorial of " + n1 + " is : " + ans1); int n2 = 12; // Using factorial(int n) method of // Guava's IntMath class int ans2 = IntMath.factorial(n2); System.out.println("factorial of " + n2 + " is : " + ans2); } } Output: factorial of 10 is : 3628800 factorial of 12 is : 479001600 Example 2 : Java // Java code to show implementation of // factorial(int n) method of Guava's // IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { static int findFact(int n) { try { // Using factorial(int n) method of // Guava's IntMath class // This should throw "IllegalArgumentException" // as n < 0 int ans = IntMath.factorial(n); // Return the answer return ans; } catch (Exception e) { System.out.println(e); return -1; } } // Driver code public static void main(String args[]) { int n = -5; try { // Function calling findFact(n); } catch (Exception e) { System.out.println(e); } } } Output: java.lang.IllegalArgumentException: n (-5) must be >= 0 Comment More infoAdvertise with us Next Article Java Guava | factorial(int n) method of IntMath Class with Examples bansal_rtk_ Follow Improve Article Tags : Java Java-Functions java-guava Guava-LongMath Practice Tags : Java Similar Reads Java Guava | Longs.factorial(int n) method with Examples The factorial(int n) method of Guava's LongMath Class returns the product of the first n positive integers, which is n!. Syntax : public static long 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: Thi 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 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.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 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 | 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 | 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 | floorPowerOfTwo() method IntMath Class The floorPowerOfTwo() method of Guava's IntMath class accepts a parameter and returns the largest power of two less than the value passed in the parameter. This is equivalent to: checkedPow(2, log2(x, FLOOR)). Syntax : public static int floorPowerOfTwo(int x) Parameters: This method accepts a single 2 min read Java Guava | binomial() method of IntMath Class The binomial(int n, int k) method of Guava's IntMath 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 an integer than the method returns Integer.MAX_VALUE or in other words the maximum value 2 min read Java Guava | isPrime() method of IntMath Class The isPrime(int n) method of Guava's IntMath class is used to check whether the parameter passed to it is a prime number or not. If the parameter passed to it is prime, then it returns True otherwise it returns False. A number is said to be Prime if it is divisible only by 1 and the number itself. S 2 min read Like