Core Java

Basic Operators: % in Java

1. Introduction

In this post, we will understand the Modulo operator – % in Java. Java Modulo operator or modulus operator is used to getting the remainder when we divide an integer with another integer. It is a binary operator as it is applied to two operands.

The % character is the modulus operator in Java. Its syntax is:

int remainder = x % y where x, y are integers

Ex:- int remainder= 9 % 5

The modulus operator always returns an integer. If we try to use the modulo operator with any other type of variable, we will get a compilation error.

2. Examples in Java using % Operator

Let’s look at a simple example of using the modulus operator to find the remainder when two integers are divided.

Example1.java

class Test {

	public static void main(String[] args) {
		int x = 10;
		int y = 3;

		int remainder = x % y;
		System.out.println("10 % 3 = " + remainder);
	}

}

Output

10 % 3 = 1

When we divide number 10 with number 3, we get remainder as 1. so modulo operator has returned the remainder.

One of the simple use cases of the modulus operator is to test if an integer is odd or even. Let us understand this with the below example.

Example2.java

import java.util.Scanner;
 class Test {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("Please enter an integer:");
		
		int in = scanner.nextInt();
		scanner.close();
		
		if (in % 2 == 0) { //using mod operator
			System.out.println("You entered an even number");
		}else {
			System.out.println("You entered an odd number");
		}
	}

}

In this example we are taking the integer as input from the user, if the user gives input as 6 , which is an even number then

Output

Please enter an integer:6
You entered an even number

if the user gives input as 45, which is an odd number then

Output

Please enter an integer:45
You entered an odd number

Now in our last example, let us understand the modulo operator when used with negative numbers.

When a modulus operator is used with negative integers, the output retains the sign of the dividend. if x % y is our computation, here x is the dividend.

Example3.java

class Test
{
	public static void main(String[] args) {
		
		int result1 = 10%3;
		int result2= -10%3;
		int result3= 10%-3;
		System.out.println(result1); //case1
		System.out.println(result2); //case2
		System.out.println(result3); //case3
	}
}

Output

1
-1
1

In case 1, the result1 is 1 because the dividend is 10 which is a positive number. So the modulo operator returns the positive remainder.

% in Java

In case 2, the result2 is -1 because the dividend is -10, which is a negative number. So the modulo operator returns the negative remainder.

In case 3, the result3 is 1 because the dividend is 10, which is a positive number. So the modulo operator returns the positive remainder.

3. Summary

In this post, we have discussed the Modulo operator in java. Using the modulo operator we have seen how to check an integer whether it is even or odd. Then we have understood the operator using negative operands, and positive operands.

You can also take a look to at this Java Modulus Example.

4. Download the Source Code

This is an example for Modulo Operator – % in Java.

Download
You can download the full source code of this example here: Basic Operators: % in Java

Shaik Ashish

He completed his Bachelors Degree in Computer Applications. He is a freelancer, writer, Microsoft Certified in Python and Java. He enjoys Writing, Teaching, Coding in Java and Python, Learning New Technologies and Music.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button