# Java mod examples

Both remainder and modulo are two similar operations; they act the same when the numbers are positive but much differently when the numbers are negative. In Java, we can use `Math.floorMod()` to describe a modulo (or modulus) operation and `%` operator for the remainder operation.

See the result:

``````
| rem & +divisor| rem & -divisor | mod & +divisor | mod & -divisor |
| :-------------| :------------- | :------------- | :--------------|
| -5 rem 3 = -2 | -5 rem -3 = -2 | -5 mod 3 =  1  | -5 mod -3 = -2 |
| -4 rem 3 = -1 | -4 rem -3 = -1 | -4 mod 3 =  2  | -4 mod -3 = -1 |
| -3 rem 3 =  0 | -3 rem -3 =  0 | -3 mod 3 =  0  | -3 mod -3 =  0 |
| -2 rem 3 = -2 | -2 rem -3 = -2 | -2 mod 3 =  1  | -2 mod -3 = -2 |
| -1 rem 3 = -1 | -1 rem -3 = -1 | -1 mod 3 =  2  | -1 mod -3 = -1 |
|  0 rem 3 =  0 |  0 rem -3 =  0 |  0 mod 3 =  0  |  0 mod -3 =  0 |
|  1 rem 3 =  1 |  1 rem -3 =  1 |  1 mod 3 =  1  |  1 mod -3 = -2 |
|  2 rem 3 =  2 |  2 rem -3 =  2 |  2 mod 3 =  2  |  2 mod -3 = -1 |
|  3 rem 3 =  0 |  3 rem -3 =  0 |  3 mod 3 =  0  |  3 mod -3 =  0 |
|  4 rem 3 =  1 |  4 rem -3 =  1 |  4 mod 3 =  1  |  4 mod -3 = -2 |
|  5 rem 3 =  2 |  5 rem -3 =  2 |  5 mod 3 =  2  |  5 mod -3 = -1 |
``````

In a nutshell:

• `Remainder (rem)“ = The result has the same sign (+ or -) as the dividend (first operand).
• `Modulo (mod)“ = the result has the same sign (+ or -) as the divisor (second operand).

**Further Reading- **Wikipedia – Modulo Operation

## 1. Remainder vs Modulo

1.1 Remainder Operator `%`

``````
dividend rem divisor = remainder

8 % 3 = 2

dividend(8) rem(%) divisor(3) = remainder(2)
``````

Let see an example to print each remainder from dividend -10 to 10 and a divisor of 3.

JavaModExample1.java
``````
package com.mkyong;

import java.util.Arrays;
import java.util.List;

public class JavaModExample1 {

public static void main(String[] args) {

int divisor = 3;

List<Integer> list = Arrays.asList(-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

String format = "%3d rem %1d = %2d";
for (Integer dividend : list) {
String result = String.format(format, dividend, divisor, dividend % divisor);
System.out.println(result);
}

}

}
``````

Output

``````
-10 rem 3 = -1
-9 rem 3 =  0
-8 rem 3 = -2
-7 rem 3 = -1
-6 rem 3 =  0
-5 rem 3 = -2
-4 rem 3 = -1
-3 rem 3 =  0
-2 rem 3 = -2
-1 rem 3 = -1
0 rem 3 =  0
1 rem 3 =  1
2 rem 3 =  2
3 rem 3 =  0
4 rem 3 =  1
5 rem 3 =  2
6 rem 3 =  0
7 rem 3 =  1
8 rem 3 =  2
9 rem 3 =  0
10 rem 3 =  1
``````

For `divisor = -3`

``````
-10 rem -3 = -1
-9 rem -3 =  0
-8 rem -3 = -2
-7 rem -3 = -1
-6 rem -3 =  0
-5 rem -3 = -2
-4 rem -3 = -1
-3 rem -3 =  0
-2 rem -3 = -2
-1 rem -3 = -1
0 rem -3 =  0
1 rem -3 =  1
2 rem -3 =  2
3 rem -3 =  0
4 rem -3 =  1
5 rem -3 =  2
6 rem -3 =  0
7 rem -3 =  1
8 rem -3 =  2
9 rem -3 =  0
10 rem -3 =  1
``````

With the remainder `%`, the result has the same sign as the dividend (first operand).

1.2 Modulo with `Math.floorMod`

``````
dividend mod divisor = modulus

8 mod 3 = 2

Math.floorMod(8, 2) = 2

dividend(8) mod(%) divisor(3) = modulus(2)
``````

Let see an example to print each modulo from dividend -10 to 10 and a divisor of 3.

JavaModExample2.java
``````
package com.mkyong;

import java.util.Arrays;
import java.util.List;

public class JavaModExample2 {

public static void main(String[] args) {

int divisor = 3;

List<Integer> list = Arrays.asList(-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

String format = "%3d mod %1d = %2d";
for (Integer dividend : list) {
String result = String.format(format, dividend, divisor, Math.floorMod(dividend,divisor));
System.out.println(result);
}

}

}

``````

Output

``````
-10 mod 3 =  2
-9 mod 3 =  0
-8 mod 3 =  1
-7 mod 3 =  2
-6 mod 3 =  0
-5 mod 3 =  1
-4 mod 3 =  2
-3 mod 3 =  0
-2 mod 3 =  1
-1 mod 3 =  2
0 mod 3 =  0
1 mod 3 =  1
2 mod 3 =  2
3 mod 3 =  0
4 mod 3 =  1
5 mod 3 =  2
6 mod 3 =  0
7 mod 3 =  1
8 mod 3 =  2
9 mod 3 =  0
10 mod 3 =  1
``````

For `divisor = -3`

``````
-10 mod -3 = -1
-9 mod -3 =  0
-8 mod -3 = -2
-7 mod -3 = -1
-6 mod -3 =  0
-5 mod -3 = -2
-4 mod -3 = -1
-3 mod -3 =  0
-2 mod -3 = -2
-1 mod -3 = -1
0 mod -3 =  0
1 mod -3 = -2
2 mod -3 = -1
3 mod -3 =  0
4 mod -3 = -2
5 mod -3 = -1
6 mod -3 =  0
7 mod -3 = -2
8 mod -3 = -1
9 mod -3 =  0
10 mod -3 = -2
``````

With the modulo, the result has the same sign as the divisor (second operand).

## 2. Common pitfalls – Find Odd numbers.

This example uses `%` remainder to check if a given number is an odd number. For negative numbers, it can lead to an unexpected results.

JavaModExample3.java
``````
package com.mkyong;

import java.util.Arrays;
import java.util.List;

public class JavaModExample3 {

public static void main(String[] args) {

int divisor = 2;
List<Integer> list = Arrays.asList(-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

String format = "%3d %% %2d = %2d, isOdd() = %6b";
for (Integer dividend : list) {
String result = String.format(format, dividend, divisor, dividend % divisor, isOdd(dividend));
System.out.println(result);
}

}

private static boolean isOdd(int number) {
return number % 2 == 1;
}

}

``````

Output

``````
-10 %  2 =  0, isOdd() =  false
-9 %  2 = -1, isOdd() =  false
-8 %  2 =  0, isOdd() =  false
-7 %  2 = -1, isOdd() =  false
-6 %  2 =  0, isOdd() =  false
-5 %  2 = -1, isOdd() =  false
-4 %  2 =  0, isOdd() =  false
-3 %  2 = -1, isOdd() =  false
-2 %  2 =  0, isOdd() =  false
-1 %  2 = -1, isOdd() =  false
0 %  2 =  0, isOdd() =  false
1 %  2 =  1, isOdd() =   true
2 %  2 =  0, isOdd() =  false
3 %  2 =  1, isOdd() =   true
4 %  2 =  0, isOdd() =  false
5 %  2 =  1, isOdd() =   true
6 %  2 =  0, isOdd() =  false
7 %  2 =  1, isOdd() =   true
8 %  2 =  0, isOdd() =  false
9 %  2 =  1, isOdd() =   true
10 %  2 =  0, isOdd() =  false
``````

For `%` remainder operator, the result has the sign (+ or -) of dividends (first operand). For example `-3 % 2 = -1`, the above `isOdd()` will return `false`.

One of the possible answers is test if the remainder is not equal to zero ( 0 has no sign (+ or -)).

``````
private static boolean isOdd(int number) {
return number % 2 != 0;
}
``````

Output

``````
-10 %  2 =  0, isOdd() =  false
-9 %  2 = -1, isOdd() =   true
-8 %  2 =  0, isOdd() =  false
-7 %  2 = -1, isOdd() =   true
-6 %  2 =  0, isOdd() =  false
-5 %  2 = -1, isOdd() =   true
-4 %  2 =  0, isOdd() =  false
-3 %  2 = -1, isOdd() =   true
-2 %  2 =  0, isOdd() =  false
-1 %  2 = -1, isOdd() =   true
0 %  2 =  0, isOdd() =  false
1 %  2 =  1, isOdd() =   true
2 %  2 =  0, isOdd() =  false
3 %  2 =  1, isOdd() =   true
4 %  2 =  0, isOdd() =  false
5 %  2 =  1, isOdd() =   true
6 %  2 =  0, isOdd() =  false
7 %  2 =  1, isOdd() =   true
8 %  2 =  0, isOdd() =  false
9 %  2 =  1, isOdd() =   true
10 %  2 =  0, isOdd() =  false
``````
Note
Which one to choose, remainder `%` or modulo `Math.floorMod`? It depends on what you are going to build; both act the same for positive numbers but remember to take care of the negative result to avoid the common pitfalls like the `isOdd()` above.

## References

#### mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by