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 IllegalArgumentException if a < 0 or b < 0.
Example 1 :
// Java code to show implementation of // gcd(int a, int b) 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 a1 = 64; int b1 = 36; // Using gcd(int a, int b) method // of Guava's IntMath class int ans1 = IntMath.gcd(a1, b1); System.out.println("GCD of a1 & b1 is: " + ans1); int a2 = 23; int b2 = 15; // Using gcd(int a, int b) method // of Guava's IntMath class int ans2 = IntMath.gcd(a2, b2); System.out.println("GCD of a2 & b2 is: " + ans2); } } |
chevron_right
filter_none
Output:
GCD of a1 & b1 is: 4 GCD of a2 & b2 is: 1
Example 2 :
// Java code to show implementation of // gcd(int a, int b) 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 a1 = -5; int b1 = 15; try { // Using gcd(int a, int b) method // of Guava's IntMath class // This should throw "IllegalArgumentException" // as a1 < 0 int ans1 = IntMath.gcd(a1, b1); } catch (Exception e) { System.out.println(e); } } } |
chevron_right
filter_none
Output:
java.lang.IllegalArgumentException: a (-5) must be >= 0
Note: The method returns 0 if a == 0 && b == 0.
Recommended Posts:
- Java Guava | mod() method of IntMath Class
- Java Guava | mean() method of IntMath Class
- Java Guava | pow(int b, int k) method of IntMath Class
- Java Guava | isPowerOfTwo() method IntMath Class
- Java Guava | floorPowerOfTwo() method IntMath Class
- Java Guava | ceilingPowerOfTwo() method of IntMath Class
- Java Guava | binomial() method of IntMath Class
- Java Guava | isPrime() method of IntMath Class
- Java Guava | factorial(int n) method of IntMath Class with Examples
- Java Guava | sqrt(int x, RoundingMode mode) method of IntMath Class
- IntMath Class | Guava | Java
- Java Guava | IntMath.checkedSubtract(int a, int b) method with Examples
- Java Guava | IntMath.checkedPow(int b, int k) method with Examples
- Java Guava | IntMath.checkedAdd(int a, int b) method with Examples
- Java Guava | IntMath.checkedMultiply(int a, int b) method with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



