Table of Content |
---|
What is a Java modulus operator? |
More applications of Modulus Operator |
Key Takeaways |
FAQs |
The modulo operator is an arithmetic operator indicated by the percent symbol - "%".
Syntax:
A % B
Where A denotes the dividend and B represents the divisor.
Let us understand Modulus Operator in Java.
We are probably familiar with the phrase modulus. Additionally, it is one of the most frequently requested interview questions for C, C++, Java, or Python.
Let's brush up on the modulus concept in this article and then look into the Java implementation process.
Let's get started!
What is a Java modulus operator?
After division, the modulus operator (%) returns the remainder of the two numbers.
If you are given two numbers, say A and B, where A is the dividend and B is the divisor, then A mod B indicates if there is a remainder after dividing A by B.
Syntax of the Modulus in Java
A % B
Where A represents the dividend and B is the divisor.
Now that we are familiar with the syntax let's understand how to apply it in the Java program.
public static void main(String args[]){ int A, B, modulusResult; A = 57; //This is Dividend B = 43; //This is Divisor System.out.println("A = " + A + " B = " + B); modulusResult = A % B; //This is the modulus we get after dividing Dividend by Divisor. System.out.println("The result after modulus operation is : "+ modulusResult); }
Output:
A = 57 B = 43
The result after modulus operation is : 14
How to use modulus in java ?
This is the approach being followed in the above code.
- Initialize Dividend, Divisor, and Modulus variables.
- Assign an integer variable to the Dividend variable(A) percent Divisor variable(B).
- Store the modulus result in a variable (modulusResult) by dividing the Dividend variable(A) with Divisor variable(B).
- Print the value of the modulusResult variable.
The modulus operation produces the following result: 14

This is how to use modulus in Java application.
More applications of Modulus Operator
Additionally, the modulus operator can be used to determine whether a number is even or odd. Let us examine how!
public static void main(String args[]){ int inputNumber1 = 57; int inputNumber2 = 108; checkEvenOrOdd(inputNumber1); checkEvenOrOdd(inputNumber2); } public static boolean checkEvenOrOdd(int inputNumber){ if(inputNumber%2 == 0){ System.out.println("The number - " + inputNumber + " is an even number"); return true; }else { System.out.println("The number - " + inputNumber + " is an odd number"); return false; } }
As a result, we can use the modulus operator in Java in a wide variety of situations.
Modulus in Java may also be used to determine -
- whether a number is a prime number or not.
- To keep track of the index of the next available position in a circular array.
- To construct logic such as reversing a number.
- Finding palindromes.
- To determine the final digit of a number.
- Whether a year is a leap year.
- Or to calculate the remaining total of amounts or something similar.
Key Takeaways
- Modulus is also referred to as the remainder operator and is denoted by the percent '%' sign.
- The modulus operator applies to variables of integral types such as byte, short, int, and long, and variables of floating-point types like float and double.
It's straightforward. Just remember - After division, the Modulo or Remainder Operator (%) returns the remainder of the two numbers.
The modulus operator is a critical operator that may be useful in real-world development and answering coding questions during interviews.
Time Complexity :
Modular functions, like other arithmetic operations, typically take Constant time / O(1) time.
FAQs
What Is the Distinction Between a Modulo and a Division Operator?
The primary distinction between the modulo operator (%) and division operator (/) is that the modulo (%) operator returns the remainder, whereas the division (/) operator returns the quotient.
For a more precise idea, refer to this image which is an example of Quotient, Remainder, Modulus Operator, Division Operator, Dividend, and Divisor.

This brings us to the end of the article - Modulus Operator (Modulo or Remainder) in Java. We have learnt how to use modulus in Java.
Hope it helped!
Cheers!
Happy Coding.
About the Author
This article was authored by Rawnak.
Comments are closed.