The modulus operator “%” comes in handy in various logic-building situations in Java. The most common application of this operator is to evaluate the “even” or “odd” numbers based on the retrieved remainder. Moreover, it is also effective while dealing with palindromes, reversing the numbers, or returning a precise value (in the case of floating-point numbers).
This article will demonstrate the concept and working of the “%” modulus operator in Java.
What Does “%” Mean in Java?
The “%” refers to the modulus operator that divides the left operand by the right operand and gives the corresponding remainder. For instance, “7%3” retrieves the value as “1” since upon dividing 7 by 3, “1” is left as the remainder.
Syntax
Num1 % Num2
In this syntax, “Num1” corresponds to a dividend, and “Num2” is a divisor.
Example 1: Applying the “%” Operator Upon the Integer Values
This example applies the modulus operator “%” to divide the two specified integers and return the corresponding remainder:
public class ModulusOperator {
public static void main(String args[]) {
int val1 = 10, val2 = 7;
int out = val1 % val2;
System.out.println("Resultant Value -> " + out);
}}
In the above code lines:
- Initialize the two stated integers.
- Now, apply the modulus “%” operator to divide the left operand (dividend) by the right one (divisor) and retrieve the remainder.
Output

This output verifies that upon dividing 10 by 7, “3” is returned as a remainder.
Example 2: Applying the “%” Operator Upon the Float Values
In this particular demonstration, the discussed operator can be implemented upon the float values:
public class ModulusOperator {
public static void main(String args[]) {
float val1 = 10.5f, val2 = 8.3f;
float out = val1 % val2;
System.out.println("Resultant Value -> " + out);
}}
According to this code block, define the two float values. After that, utilize the modulus operator “%” to fetch the remainder retrieved from dividing both the float values.
Output

The resultant precise float is returned accordingly.
Example 3: Applying the “%” Operator Upon the User Input Values Based on a Condition
The user input values will be dealt with using the “%” operator in this approach. It is such that the user input integers will be evaluated as even or odd using “if/else” statements:
import java.util.Scanner;public class ModulusOperator { public static void main(String args[]) { Scanner input = new Scanner(System.in); System.out.println("Enter the integer: "); int x = input.nextInt(); if (x % 2 == 0) System.out.println("Input Number is Even!"); else System.out.println("Input Number is Odd!"); input.close();}}
Perform the below-provided steps based on this code:
- Import the “java.util.Scanner” package to work with the “Scanner” class, thereby ensuring user input.
- In “main”, create a Scanner object utilizing the “new” keyword and the “Scanner()” constructor.
- Now, input the integer from the user via the applied “nextInt()” method.
- In the next step, associate the modulus operator “%” with the user input such that if the user input integer is even (integer completely divisible by 2), the “if” statement executes.
- Otherwise, the execution of the “else” statement is carried out.
- Lastly, close the Scanner.
Output
This output evaluated the user input values in both conditions appropriately.

Conclusion
The “%” corresponds to the modulus operator that divides the left-hand operand by the right-hand operand and gives the corresponding remainder. This operator is compatible with multiple data types and evaluates the user input values as well.