Java Modulo Operator – Modulus Operator in Java

Filed Under: Java

Java Modulo operator or modulus operator is used to getting the remainder when we divide an integer with another integer.


Java Modulo Operator Syntax

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


int remainder = int % int

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.


Modulus Operator Example

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


package com.journaldev.java;

public class JavaModuloOperator {

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

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

}

Using Modulus Operator to check if an integer is even or odd

One of the simple use cases of the modulus operator is to test if an integer is odd or even.


package com.journaldev.java;

import java.util.Scanner;

public class JavaModuloOperator {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.println("Please enter an integer:");
		
		int in = scanner.nextInt();
		scanner.close();
		
		if (in % 2 == 0) {
			System.out.println(String.format("%d is even.", in));
		}else {
			System.out.println(String.format("%d is odd.", in));
		}
	}

}

Output:


Please enter an integer:
10
10 is even.

Please enter an integer:
9
9 is odd.

Here are the utility methods to check if the integer is even or odd.


public static boolean isEven(int x) {
	return x % 2 == 0;
}

public static boolean isOdd(int x) {
	return x % 2 != 0;
}

Using Modulus Operator to check if an integer is divided by another integer

We can use the modulo operator to check if an integer is divided by another integer or not.


package com.journaldev.java;

import java.util.Scanner;

public class JavaModuloOperator {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.println("Please enter the first integer:");

		int x = scanner.nextInt();

		System.out.println("Please enter the second integer:");

		int y = scanner.nextInt();
		scanner.close();

		if (x % y == 0) {
			System.out.printf("%d is divided by %d", x, y);
		} else {
			System.out.printf("%d is not divided by %d", x, y);
		}

	}

}

Output:


Please enter the first integer:
10
Please enter the second integer:
5
10 is divided by 5

Here is the implementation of a utility method to check if an integer is divided by another integer or not.


public static boolean isDivided(int x, int y) {
	return x % y == 0;
}

Modulus Operator with negative integers

When modulus operator is used with negative integers, the output retains the sign of the dividend.


jshell> 10 % 3
$66 ==> 1

jshell> -10 % 3
$67 ==> -1

jshell> 10 % -3
$68 ==> 1
Java Modulo Operator Examples

Java Modulo Operator Examples

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages