Java Guava | mod() method of IntMath Class
The mod(int x, int m) method of Guava’s IntMath class accepts two parameters x and m and used to calculate the value of x modulus under m.
Syntax :
public static int mod(int x, int m)
Parameters: This method accepts two parameters x and m which are of integer types and calculate x modulo m.
Return Value : The method returns x mod m that will be a non-negative value less than m.
Exception : The method mod(int x, int m) throws ArithmeticException if m <= 0.
Below examples illustrate the mod(int x, int m) method:
Example 1 :
// Java code to show implementation of // mod(int x, int m) 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 x1 = -84; int m1 = 5; int ans1 = IntMath.mod(x1, m1); // Using mod(int x, int m) // method of Guava's IntMath class System.out.println(x1 + " mod " + m1 + " is : " + ans1); int x2 = 14; int m2 = 6; int ans2 = IntMath.mod(x2, m2); // Using mod(int x, int m) // method of Guava's IntMath class System.out.println(x2 + " mod " + m2 + " is : " + ans2); } } |
Output :
-84 mod 5 is : 1 14 mod 6 is : 2
Example 2 :
// Java code to show implementation of // mod(int x, int m) method of Guava's // IntMath class import java.math.RoundingMode; import com.google.common.math.IntMath; class GFG { static int findMod(int x, int m) { try { // Using mod(int x, int m) // method of Guava's IntMath class // This should throw "ArithmeticException" // as m <= 0 int ans = IntMath.mod(x, m); // Return the answer return ans; } catch (Exception e) { System.out.println(e); return -1; } } // Driver code public static void main(String args[]) { int x = 14; int m = -3; try { // Function calling findMod(x, m); } catch (Exception e) { System.out.println(e); } } } |
Output :
java.lang.ArithmeticException: Modulus -3 must be > 0
Reference :
https://google.github.io/guava/releases/20.0/api/docs/com/google/common/math/IntMath.html#mod-int-int-
Recommended Posts:
- Java Guava | pow(int b, int k) method of IntMath Class
- Java Guava | gcd(int a, int b) method of IntMath Class
- Java Guava | mean() method of IntMath Class
- Java Guava | isPowerOfTwo() method IntMath Class
- Java Guava | floorPowerOfTwo() method IntMath Class
- Java Guava | isPrime() method of IntMath Class
- Java Guava | ceilingPowerOfTwo() method of IntMath Class
- Java Guava | binomial() 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.checkedAdd(int a, int b) method with Examples
- Java Guava | IntMath.checkedPow(int b, int k) 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.



