# Java Mod Example

In Java, mod (or modulo) is an operator that is used to determine the remainder. Java provides Math.floorMod() method that can be used instead of a modulo (or modulus) operation and % operator to perform the remainder operation. Here, a point to note is that they act the same when the numbers are positive but much differently when the numbers are negative.

In sort,

• Remainder = The result has the same sign (+ or -) as the dividend.
• Modulo = The result has the same sign (+ or -) as the divisor.

Remainder.java

Output:

```When Divisor is 3:
-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

When Divisor is -3:
-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
```

Modulo.java

Output:

```When Divisor is 3:
-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

When Divisor is -3:
-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
```

## Using Math.floorMod() Method

The Java Math class provides the floorMod() method to determine the modulo of an integer argument. The method accepts two parameters (dividend and divisor) of type int. The method returns the floor modulus x - (floorDiv(x, y) * y).

### Syntax:

The method throws ArithmaticException if the divisor (y) is 0.

#### Note that the floor modulus has the same sign as the divisor y and is in the range of -abs(y) < r < +abs(y). The floorMod() and floorDiv() has the following relationship.

Also, note that floorMod() and % operator are not the same, there is a slight difference between them. The difference is due to floorDiv() method because it returns an integer less than or equal to the quotient and the operator returns the integer closest to zero.

### Remember:

• If both the arguments have the same sign, the result of floorMod() method and % operator are the same.
• If both the arguments have different signs, the result floorMod() method and % operator will be different.

For example, consider the following:

• floorMod(5, 2) == 1; and (5 % 2) == 1
• floorMod(+5, -2) == -1; and (+5 % -2) == 1
• floorMod(-5, +2) == 1; and (-5 % +2) == -1
• floorMod(-5, -2) == -1; and (-5 % -2) == -1

If the sign of arguments is unknown and we required a positive modulus, we can use the following statement.

FloorModExample.java

Output:

```3 % -2 = 1
floorMod(3, -2) = -1
```

#### Note: Which one to choose, remainder % or modulo Math.floorMod(). It depends on what we are going to build; both act the same for positive numbers but remember to take care of the negative result to avoid the common drawbacks as we discussed above.

Therefore, we can conclude that mod and remainder are not the same.

Next TopicStone Game in Java

### Feedback   