Open In App
Related Articles

Java Guava | factorial(int n) method of IntMath Class with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

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 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 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

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!

Last Updated : 19 Sep, 2023
Like Article
Save Article
Previous
Next
Similar Reads