Javatpoint Logo
Javatpoint Logo

Java Modulo

In this section, we will discuss the Java modulo operator.

Java Modulo Operator

In mathematics, there is basically four arithmetic operators addition (+), subtraction (-), multiplication (*), and division (/). In programming, except for these four operators, there is another operator called modulo or modulus operator. It is represented by the percentage symbol (%).

Java Modulo

It is used to determine the remainder. It requires two operands. It divides the left-hand operand by the right-hand operand and gives the remainder. For example:

Java Modulo

We can also use it with the assignment operator (=). For example,

It takes modulus using two operands and assigns the result to left operand (a).

If it appears in an expression, we must solve the expression left to right. For example, consider the following expression.

If the number is completely divided, it returns gives 0 as the result. For example, 25%5 gives 0.

Let's see some other examples of modulo operator.






Uses of Modulo Operator

  • The most common use case for the modulo operator is to find out if a given number is odd or even.
  • Another use of the modulo operator is to keep track of the index of the next free spot in a circular array.

For a programmer, it is very important to know how to use the modulo operator because it plays a vital role to build logic like reverse a number, find even odd, palindrome, and many more. We can also use it to find the last digit of a number.

Suppose, the given number is 5678 and we have to find the last digit of the number. As we know the modulo operator determines the remainder, so we will divide the given number by 10. Therefore, we get 8 as the remainder and the last digit.

We can also use the modulo operator with floating-point numbers. For example,

Using Modulo Operator in a Java Programs

The following Java program demonstrates the use of the modulo operator in Java.


Remainder: 1


Remainder: 0.7999999999999998

Difference Between Modulo and Division Operator

The major difference between modulo and division operators is that the modulo (%) operator gives the remainder while the division (/) operator gives the quotient.

Java Modulo

The following Java program will clear the concept of modulo and division operator.


Quotient: 6
Remainder: 1

Java Program to Find Remainder Without Using the Modulo Operator


Remainder: 6

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA