Java Modulus / Modulo operator – Examples and Uses

The Java modulus ‘%’ operator is one of numerous operators built into the Java programming language. The operator is used to calculate the remainder of the division between two numbers. First, let us discuss how the operator works.

How to use the ‘%’ operator

The ‘%’ operator requires two operands. The first is the number on which the operator will be applied upon and the second is the divisor. The result should be the remainder that would remain if we had divided the first number by the divisor. An example usage of the operator in Java can be as follows:

package com.nullbeans.basics;

public class Main {

    public static void main(String[] args) {

        int result = 23 % 6;

        System.out.println("result = " + result);

    }
}

In this case the result will be 5.

result = 5


Process finished with exit code 0

Another way to describe the modulus operation is that it finds out the first remaining number which is smaller than the divisor, if we repeatedly subtracted the divisor from the numerator. 

For example:

23 - 6 = 17  // Still larger than the divisor (6), try again...

17 - 6 = 11 // Still larger than the divisor (6), try again...

11 - 6 = 5  // Number is smaller than the divisor (6). Stop.

Uses of the modulo operator

The ‘%’ operator can be used in many situations within a Java program. Let us check some use cases:

Checking if a number is even or odd:

package com.nullbeans.basics;

public class Main {

    public static void main(String[] args) {

        int userInput = 32232;
        
        isEvenNumber(userInput);

    }
    
    public static boolean isEvenNumber(int userInput){
        if(userInput%2 == 0){
            System.out.println("Input is an even number");
            return true;
        }else {
            System.out.println("Input is an odd number");
            return false;
        }

    }
}

Checking for even distribution:

In some situations, it is important to insure equality. For example, if a specific number of items can be evenly distributed on different channels/nodes/entities. In this case, the condition can be calculated with the modulus operator:

boolean isEvenDistribution = numOfItems % numOfEntities == 0;

Converting seconds to hours, minutes and seconds

In this example, we take an input of the total amount of seconds and we calculate the number of hours, remaining minutes and seconds.

package com.nullbeans.basics;

public class Main {

    public static void main(String[] args) {

        int seconds = 123456;

        printTime(seconds);

    }

    public static void printTime(int seconds){

        int remainingSeconds = seconds % 60;

        int minutes = seconds / 60;

        int remainingMinutes = minutes % 60;

        int hours = minutes / 60;

        System.out.println("The required time is "  + hours + " hours " + remainingMinutes + " minutes and " + remainingSeconds + " seconds");
    }

}

The output of the program above would be:

The required time is 34 hours 17 minutes and 36 seconds

Further info

The operator can be used in many other situations. For example, to find out if a number is a prime number, or in an accounting application that is used to calculate money installments and remaining amounts.