How to use the Java modulo operator
When division doesn’t work out perfectly, you’re left with a remainder. The Java modulo operator is there to account for this in your code. The operator is significantly more reliable than other solutions and is primarily used to determine whether numbers are even or odd.
What is the Java modulo operator?
In all the common programming languages, you can perform the basic arithmetic operations of addition, subtraction, multiplication and division. You can also use Java operators to display and solve complex calculations. When it comes to division in particular, there is always the risk that there is a remainder. For example, if you divide 11 by 4, you’ll have a remainder of 3 (2 x 4 = 8, 11 - 8 = 3). This can lead to problems in programming.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
That’s what the Java modulo operator is for. It’s also known as the remainder operator, as it determines and returns the remainder after two numbers are divided. There are various situations in which it is extremely important. The operator can be used, for example, to find out whether a number is even or odd or whether a number is prime. While there are also other ways to determine the remainder, they quickly reach their limits. The following example will illustrate that.
What is the remainder operator used for?
Put simply, division involves checking how many times a divisor fits into a dividend. The result is the quotient. If the dividend and divisor are both stored in the primitive Java data type int (or integer), the remainder can easily be expressed without Java modulo. The code would look as follows:
public class Main {
public static void main(String[] args) {
int dividend = 11;
int divisor = 4;
int remainder = dividend - (divisor * (dividend / divisor));
System.out.println("The remainder is: " + remainder);
}
}
javaWe can then use the Java command System.out.println
to view the result:
The remainder is: 3
javaThis option is a bit unwieldy but is in theory possible. However, we’ll run into problems as soon as at least one of the operands is a float or double, that is a floating number data type. In this case, the same calculation will give us a different result:
public class Main {
public static void main(String[] args) {
int dividend = 11;
double divisor = 4;
double remainder = dividend - (divisor * (dividend / divisor));
System.out.println("The remainder is: " + remainder);
}
}
javaThe remainder is: 0.0
javaWhat is the syntax of modulo in Java?
The Java modulo operator, on the other hand, will always return the exact remainder. It performs division but only shows the remainder, not the quotient. It’s initialized with a percent sign, meaning its syntax looks as follows:
Dividend % Divisor
javaJava modulo would look as follows when used in the above example:
public class Main {
public static void main(String[] args) {
int dividend = 11;
double divisor = 4;
double remainder = dividend % divisor;
System.out.println("The remainder is: " + remainder);
}
}
javaThe output is then:
The remainder is: 3.0
javaHow to determine whether a number is odd or even
Java modulo can be used to find out whether a number is even or odd. It follows from simple logic: If you divide any number by 2 and the remainder is 0, it’s even. Otherwise it’s odd. We’ll illustrate this with an if-else statement.
public class Main {
public static void main(String[] args) {
int dividend = 11;
if (dividend % 2 == 0) {
System.out.println(dividend + " is an even number.");
}
else {
System.out.println(dividend + " is an odd number.");
}
}
}
javaAs expected, we get the following result:
11 is an odd number.
javaHow to determine the remainder of a negative number
You can also use Java modulo to get the remainder when the dividend or divisor is negative. Here’s a simple example:
public class Main {
public static void main(String[] args) {
int dividend = -11;
int divisor = 4;
int remainder = dividend % divisor;
System.out.println("The remainder is: " + remainder);
}
}
javaSince the dividend is negative, the remainder is negative as well:
The remainder is: -3
javaBut you might not always want to get a negative result. If you want to return a positive remainder, you can modify the code a bit. First check if the remainder is less than 0. If so, add the divisor and you’ll get the positive remainder. Here’s how that code will look:
public class Main {
public static void main(String[] args) {
int dividend = -11;
int divisor = 4;
int remainder = dividend % divisor;
System.out.println("The remainder before is: " + remainder);
while (dividend < 0) dividend += divisor;
int positive_remainder = dividend % divisor;
System.out.println("The remainder after is: " + positive_remainder);
}
}
javaWe’ll get the following output:
The remainder before is: -3
The remainder after is: 1
java