Mod of Negative Numbers in Java

Get the Mod of Negative Numbers by Using the
%
Operator in Java 
Get the Mod of Negative Numbers by Using the
floormod()
Method 
Get the Mod of Negative Numbers by Using the
Math.abs()
Method
In Java, we can find the remainder of two numbers after division with the help of the Modulo or Remainder Operator (%
) operator.
Suppose we are given two numbers, A and B, where A is the dividend and B is the divisor. When we operate A mod B, we get a Remainder when A is divided by B.
If we deal with negative numbers, we sometimes get the wrong results. This article will discuss different ways to find the mod of negative numbers in Java.
If we have negative numbers and use the %
operator on them, we will get the result dependent on the left operand’s sign. If we have the left operand as positive, we will get the result as positive, and if the left operand is negative, we will get the result as negative.
To solve the above problem, we can find the mod of negative numbers by using the %
operator, the floorMod()
method, and the Math.abs()
method. Let us discuss each of the approaches one by one.
Get the Mod of Negative Numbers by Using the %
Operator in Java
We can find the mod of negative number by first adding the mod factor until we get into the positive domain and then invoke the %
operator. With the help of this method, we always have the mod factor greater than the output.
Let us look at the code below to understand how it works.
public class Main
{
public static void main(String args[])
{
int x=4;
int y=3;
int res=x%y;
System.out.println("Mod value before operation = " + res);
while (x<0) x+=y;
int mod_res=x%y;
System.out.println("Mod value after operation = " + mod_res);
}
}
Output:
Mod value before operation = 1
Mod value after operation = 2
Get the Mod of Negative Numbers by Using the floormod()
Method
We can also find the mod of negative number by using the floorMod()
method. With the help of this method, we get the floor modulus of the int arguments.
Let us look at the syntax of the floorMod()
function.
Syntax:
floorMod(number,mod)
When the floorMod()
function is invoked on a negative integer, we get the output with the mod factor sign. Let us look at the code below to understand how it works.
public class Main
{
public static void main(String args[])
{
int x=4;
int y=3;
int res=x%y;
System.out.println("Mod value before operation = " + res);
int mod_res=Math.floorMod(x, y); // This will have the output value as positive since the smod factor is positive
System.out.println("Mod value after operation having mod factor as positive = " + mod_res);
x=4;
y=3;
mod_res=Math.floorMod(x, y); // This will have the output value as negative since the mod factor is negative
System.out.println("Mod value after operation having mod factor as negative = " + mod_res);
}
}
Output:
Mod value before operation = 1
Mod value after operation having mod factor as positive = 2
Mod value after operation having mod factor as negative = 2
Get the Mod of Negative Numbers by Using the Math.abs()
Method
We can also find the mod of negative numbers by using the Math.abs()
method.
We will perform this task with the help of a ternary operator. This operator will yield output for both cases, whether the number is positive or negative.
Let us look at the code below to understand how it works.
public class Main
{
public static void main(String args[])
{
int x=4;
int y=3;
int res=x%y;
System.out.println("Mod value before operation = " + res);
int mod_res = (x < 0) ? (y  (Math.abs(x) % y) ) %y : (x % y);
System.out.println("Mod value after operation = " + mod_res);
}
}
Output:
Mod value before operation = 1
Mod value after operation = 2