Modulus in Java

Rupam Yadav Oct 12, 2023
  1. Use Math.floorMod() to Calculate the Mod of Two Number in Java
  2. Use the % Operator to Calculate the Mod of Two Number in Java
Modulus in Java

The modulus or modulo operator returns the remainder of the two integers after division. It is utilized in simple tasks like figuring out a number is even or not and more complex tasks like tracking the next writing position in a circular array.

Use Math.floorMod() to Calculate the Mod of Two Number in Java

The Math.floorMod(a,b) function accepts two arguments that can be of int or long type. In the function, a is the dividend while b is the divisor. It returns the floor modulus of the two arguments passed to the function. The mod of say a and b will yield a result greater than or equal to 0, and less than b.

Here in the code given below, the variable num1 is exactly divisible by num2; hence the remainder is 0. For the second scenario, we have num3 and num4 of the same sign and gives a remainder of the same sign only.

But if we consider the third scenario where the dividend num5 is positive while divisor num6 is negative, the remainder will carry the sign of the divisor. Similarly, in the last case dividend, num7 is still negative; the result carries the sign of the positive divisor only.

import java.lang.Math;

public class Main {
  public static void main(String[] args) {
    int num1 = 20, num2 = 4;
    System.out.println(Math.floorMod(num1, num2));

    int num3 = 113, num4 = 30;
    System.out.println(Math.floorMod(num3, num4));

    int num5 = 113, num6 = -30;
    System.out.println(Math.floorMod(num5, num6));

    int num7 = -113, num8 = 30;
    System.out.println(Math.floorMod(num7, num8));
  }
}

Output:

0
23
-7
7

Use the % Operator to Calculate the Mod of Two Number in Java

The remainder % operator is also used for the remainder operation. There is a subtle difference between mod and remainder. As mentioned above the resultant for the mod of a and b is always greater than or equal to 0, and less than the b(divisor).

If we consider the example given below using num1 % num2, we determine if a num1 is even or odd. If the condition where the remainder of num1 % num2 is equal to 0, the number is even else odd. In the % operation, the resultant carries the sign of the dividend, which is visible if we take num3 % num2. The equation -23 % 2 is equal to -1 hence the sign of the dividend.

Similarly, if the dividend is positive, the resultant remainder is positive. For instance num1 % num4 results in positive remainder. The equation 17 % -3 is equal to 2.

public class ModTest {
  public static void main(String args[]) {
    int num1 = 17;
    int num2 = 2;

    boolean result = false;

    if (result = (num1 % num2) == 0) {
      System.out.println("Number " + num1 + " is even");
    } else {
      System.out.println("Number " + num1 + " is odd");
    }

    int num3 = -23;
    int num4 = -3;
    System.out.println("Remainder1: " + num3 % num2);
    System.out.println("Remainder2: " + num1 % num4);
  }
}

Output:

Number 17 is odd
Remainder1: -1
Remainder2: 2
Author: Rupam Yadav
Rupam Yadav avatar Rupam Yadav avatar

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

LinkedIn

Related Article - Java Math