Java Math floorMod() Method
The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to the remainder.
Syntax:
- public static int floorMod (int x, int y)
- public static long floorMod (long x, int y)
- public static long floorMod (long x, long y)
Parameters:
The parameter ‘x’ and ‘y’ represents the dividend and the divisor.
Return Value:
The floorMod () method returns the floor modulus.
Example 1:
public class JavaMathFloorModExample1 { public static void main(String[] args) { int x = 2125; int y= 10; // divides x an y and returns the remainder . System.out.println("Remainder : "+Math.floorMod(x, y)); } }
Output:
Remainder : 5
Example 2:
public class JavaMathFloorModExample2 { public static void main(String[] args) { int x = 2125; long y= -10; // divides x an y and returns the remainder System.out.println("Remainder : "+Math.floorMod(x, y)); } }
Output:
Remainder : -5
Example 3:
public class JavaMathFloorModExample3 { public static void main(String[] args) { long x = -2125; long y= 0; //it returns an Airthmetic Exception, if the Second argument is zero System.out.println(Math.floorMod(x, y)); } }
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero at java.lang.Math.floorDiv(Math.java:1086) at java.lang.Math.floorMod(Math.java:1166) at com.javaTpoint.JavaMathFloorModExample3.main (JavaMathFloorModExample3.java:8)
Example 4:
public class JavaMathFloorModExample4 { public static void main(String[] args) { long x = 2125; long y= -1; //it y is 1 or -1 then it will return 0 System.out.println(Math.floorMod(x, y)); } }
Output:
0