VOOZH about

URL: https://www.geeksforgeeks.org/java/java-guava-longs-factorialint-n-method-with-examples/

⇱ Java Guava | Longs.factorial(int n) method with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Guava | Longs.factorial(int n) method with Examples

Last Updated : 11 Jul, 2025
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: 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 long.
  • This method returns Long.MAX_VALUE if the result does not fit in a long.
Exceptions: The method factorial(int n) throws IllegalArgumentException if n is negative. Below programs illustrate the use of LongMath.factorial() method: Example 1:
Output:
factorial of 10 is : 3628800
factorial of 12 is : 479001600
Example 2 :
Output:
java.lang.IllegalArgumentException: n (-5) must be >= 0
Reference: https://guava.dev/releases/20.0/api/docs/com/google/common/math/LongMath.html#factorial-int-
Comment
Article Tags: