Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Modulo Operator in Java

The Modulo operator is used to find the remainder of the division of two numbers. It is also known as the Remainder operator. The percentage sign(%) is used to denote this operator. The basic syntax of this operator is shown below.

Dividend % Divisor

Let's take a look at some of the examples for the modulo operator.

Modulo Operator Example

Let's use the modulo operator on integers and floating numbers and view its output. We can also use the modulo operator for negative numbers.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		System.out.println("10 % 3 = " + 10 % 3);
		System.out.println("3 % 10 = " + 3 % 10);
		System.out.println("10.0 % 3.0 = " + 10.0 % 3.0);
		System.out.println("10.0 % 3 = " + 10.0 % 3);
		System.out.println("-10 % 3 = " + -10 % 3);
	}
}


10 % 3 = 1
3 % 10 = 3
10.0 % 3.0 = 1.0
10.0 % 3 = 1.0
-10 % 3 = -1

Need for Modulo Operator

The modulo operator is needed because we may lose the remainder part when using the simple division operator. If both the dividend and the divisor are integers, then we can easily calculate the remainder as shown below. An example of this case is shown below.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		int dividend = 10;
		int divisor = 3;
		int remainder = dividend - (divisor * (dividend/divisor));
		System.out.print("The remainder is: " + remainder);
	}
}


The remainder is: 1

When Dividend or Divisor is not an Integer

But if the dividend or divisor has a decimal point, then the quotient will also be a decimal number, and we can't find the remainder.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		double dividend = 10;
		int divisor = 3;
		double remainder = dividend - (divisor * (dividend/divisor));
		System.out.print("The remainder is: " + remainder);
	}
}


The remainder is: 0.0

However, the modulo operator can still find the correct remainder.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		double dividend = 10;
		int divisor = 3;
		double remainder = dividend % divisor;
		System.out.print("The remainder is: " + remainder);
	}
}


The remainder is: 1.0

ArithmeticException by Modulo Operator

The modulo operator, just like the division operator, throws an ArithmeticException if the divisor is zero.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		System.out.print("The remainder is: " + 10 % 0);
	}
}


Exception in thread "main" java.lang.ArithmeticException: / by zero
at snippet.ModuloOperatorDemo.main(ModuloOperatorDemo.java:7)

Common Use Cases of the Modulo Operator

Find Even Numbers

One of the most common use cases of the modulo operator is to check whether a number is odd or even. A number is even if we divide a number by two and the remainder is zero.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		int[] arr = {7, 5, 2, 4, 6, 19, 18, 25, 22};
		for(int i=0; i<arr.length; i++)
		{
			int num = arr[i];
			if((num % 2) == 0)
				System.out.println(num + " is even.");
			else
				System.out.println(num + " is NOT even.");
		}
	}
}


7 is NOT even.
5 is NOT even.
2 is even.
4 is even.
6 is even.
19 is NOT even.
18 is even.
25 is NOT even.
22 is even.

Restrict a Number to a certain range

We can use the modulo operator if we want to limit a number to a certain range. When the number is less than the limit, then the modulo operator returns the number itself. If the number becomes greater than or equal to the limit, the operator returns the remainder. This is very commonly used to maintain the index in a circular array.

For example, if we have a month where 1st day occurs on a Monday and we need to find the day name given the day of the month. We will move through an array of day names in a circular manner with the help of the modulo operator.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		 String[] dayNames = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
		 int dayOfTheMonth1 = 21;
		 String dayName1 = dayNames[(dayOfTheMonth1 - 1) % 7];
		 int dayOfTheMonth2 = 12;
		 String dayName2 = dayNames[(dayOfTheMonth2 - 1) % 7];
		 
		 System.out.println(dayOfTheMonth1 + " occurs on " + dayName1);
		 System.out.println(dayOfTheMonth2 + " occurs on " + dayName2);
	}
}


21 occurs on Sunday
12 occurs on Friday

Make a number completely divisible by another number

Suppose we have two numbers, X and Y, and we want to subtract the minimum value from X to make it completely divisible by Y. This minimum value would be the remainder that we get after dividing them.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		int X = 291;
		int Y = 17;
		
		int remainder = X % Y;
		X = X - remainder;
		System.out.println("The minimum number to subtract is: " + remainder);
		System.out.println("The number after subtraction is: " + X);
	}
}


The minimum number to subtract is: 2
The number after subtraction is: 289

Fetching individual digits of an integer

We can use the modulo and division operators to print each digit of a number. We can find the last digit of a number by dividing it by 10 and fetching the remainder. Then we will remove this last digit by dividing the number by 10.

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		int num = 7912064;
		while(num > 0)
		{
			int lastDigit = num % 10;
			num = num / 10;
			System.out.print(lastDigit + " ");
		}
	}
}


4 6 0 2 1 9 7

Repeat something every nth time

Suppose we have a for-loop that increases the value of a variable from 1 to 30. At every 5th iteration of the loop, we need to print a statement. The modulo operator can easily accomplish this.

public class Demo
{
	public static void main(String[] args) throws JSONException
	{
		for(int i = 1; i <= 30; i++)
		{
			if(i % 5 == 0)
				System.out.println("Do Something");
		}
	}
}


Do Something
Do Something
Do Something
Do Something
Do Something
Do Something

Convert Seconds to Minutes and Seconds

We can use the modulo operator and the division operator to convert seconds into minutes and seconds. Dividing the total seconds by 60 would give us the minutes, and the remainder gives us the remaining seconds(<60).

public class ModuloOperatorDemo
{
	public static void main(String[] args)
	{
		int seconds = 401;
		int minutes = seconds / 60;
		int remainingSeconds = seconds % 60;
		System.out.print(seconds + " seconds is equal to " + minutes +" minutes " + remainingSeconds + " seconds");
	}
}


401 seconds is equal to 6 minutes 41 seconds

Summary

The modulo operator is a simple operator that returns the remainder obtained after dividing two numbers. If the divisor is zero, then it returns an ArthimeticException as division by zero is not allowed. This operator has a lot of different use cases and we have discussed some of them in this tutorial.