VOOZH about

URL: https://www.geeksforgeeks.org/java/java-guava-gcdint-a-int-b-method-of-intmath-class/

⇱ Java Guava | gcd(int a, int b) method of IntMath Class - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Guava | gcd(int a, int b) method of IntMath Class

Last Updated : 11 Jul, 2025
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 IllegalArgumentException if a < 0 or b < 0. Example 1 :
Output:
GCD of a1 & b1 is: 4
GCD of a2 & b2 is: 1
Example 2 :
Output:
java.lang.IllegalArgumentException: a (-5) must be >= 0
Note: The method returns 0 if a == 0 && b == 0. Reference: https://guava.dev/releases/20.0/api/docs/com/google/common/math/IntMath.html#gcd-int-int-
Comment