Modulo operator in Java

In the tutorial, we will see Modulo operator in Java and how we can use it with Java.

The remainder and modulo are two similar operations; they act the same when the numbers are positive but much differently when the numbers are negative.

Modulo operator Syntax

int remainder = int % int;

We can’t use modulo with any other data type other than int. If we use it with other data types, it will give compilation error.

// compiles successfully
int rem = 20%4;
// won't compile
int rem = 10%4.0;
package com.javacodestuffs.core.strings; public class ModuloOperatorExample { public static void main(String[] args) { int a= 50; int b = 3; int rem = a%b; System.out.println("the output for 50%3 is : "+rem); } } output: the output for 50%3 is : 2

The simple use case of modulo operator in java.

check number is even or odd.
package com.javacodestuffs.core.strings; public class ModuloOperatorMain { public static void main(String[] args) { int num = 50; if (num % 2 == 0) { System.out.println("Number is even."); } else { System.out.println("Number is odd."); } } } output: Number is even.

Prime number program

Modulo operator is used to check if number is prime or not.

package com.javacodestuffs.core.modulo; public class ModuloOperatorExample3 { public static void main(String[] args) { System.out.println("Is number "+50+" is prime : "+isPrime(50)); System.out.println("Is number "+77+" is prime : "+isPrime(77)); System.out.println("Is number "+11+" is prime : "+isPrime(11)); } public static boolean isPrime(int num) { if (num <= 1) { return false; } for (int i = 2; i <= Math.sqrt(num); i++) { if (num % i == 0) { return false; } } return true; } } output: Is number 50 is prime : false Is number 77 is prime : false Is number 11 is prime : true

Modulo operator behaviour with negative integer

We use modulo operator with negative integer, it takes sign from dividend

package com.javacodestuffs.core.modulo; public class ModuloOperatorExample4 { public static void main(String[] args) { System.out.println("-19%5=" + ( - 19 % 5)); System.out.println("23%-3 =" + (23 % -3)); } } output: -19%5=-4 23%-3 =2

BigInteger mod() Method in Java

The java.math.BigInteger.mod(BigInteger big) method returns a BigInteger whose value is equal to (this BigInteger modfunction big(BigInteger passed as parameter)).This method returns the value after performing (this % big) step.The mod operation finds the remainder after division of this BigInteger by another BigInteger passed as parameter.

ava.math.BigInteger.mod(BigInteger big) and java.math.BigInteger.remainder(BigInteger val) both function finds remainder but mod operation always returns a non-negative BigInteger.

public BigInteger mod(BigInteger bigint)

Programs to explain mod() method of BigInteger class

package com.javacodestuffs.core.modulo; import java.math.BigInteger; public class ModuloOperatorBigInteger1 { public static void main(String[] args) { BigInteger b1 = new BigInteger("327654"); BigInteger b2 = new BigInteger("415"); BigInteger result = b1.mod(b2); System.out.println("Result of mod operation between " + b1 + " and " + b2 + " = " + result); } } output: Result of mod operation between 327654 and 415 = 219

When both the numbers are equal

package com.javacodestuffs.core.modulo; import java.math.BigInteger; public class ModuloOperatorBigInteger2 { public static void main(String[] args) { BigInteger b1 = new BigInteger("65635946547"); BigInteger b2 = new BigInteger("65635946547"); BigInteger result = b1.mod(b2); System.out.println("the mod operation between b1 and b2 is : " + result); } } output: the mod operation between b1 and b2 is : 0

Post/Questions related to  Modulo operator in Java

How to check if the string is whitespace java?

In this article, we have seen the Modulo operator in Java with examples. All source code in the article can be found in the GitHub repository.