## Overview

For starters, there are four arithmetic operators that you would surely have used. Those are addition (+), subtraction (-), multiplication (*), and division (/). There is another arithmetic operator that is offered in Java and various other programming languages, it is referred to by multiple names such as modulus, modulo or remainder operator but is commonly referred to as just mod in Java.

In this article, we will be discussing the distinct but very significant mod operator, how it works and how it can be very useful in numerous applications.

## Java Mod operator

The mod operator is used to determine the remainder after dividing two numbers. It is represented by the percentage symbol (%) and it is a binary operator like all other arithmetic operators. It requires two operands and it divides the left side operand (dividend) by the right-side operand (divisor) and gives the remainder. Despite being a very straightforward operator, java mode is considered very important for logic building and problem-solving. Due to this, all Java developers need to know how to use it.

**For example, see this small code snippet below demonstrating the use of Java mod:**

1. public void UsingModOperator() { 2. int number1 = 12; // Dividend 3. int number2 = 5; // Divisor 4. int result = 0; 5. result = number1 % number2; // Mod 6. System.out.println("The result of” + number1 + “ % ” + number1 + “ = " + result); 7. }

Output: The result of 12 % 5 = 2

## Operands Data types

One of the important points about the mod operator specifically for Java developers is that along with integer data type, this operator can also be used with floating-point numbers (float or double).

It is a lesser-known fact because real number division usually does not result in a remainder and this feature also does not exist in various other programming languages such as C or C++ where the mod operator only works with int operands.

The evaluated result of using the mod operator with real values will also be a real value. For example, 6.32 % 2 will evaluates to 0.32 since the division is 3 with a remainder of 0.32. This example is showing a single real value but of course, both operands can also be real values, for instance, 12.7 % 2.1 will evaluate to 0.4 since the division is 6 with a remainder of 0.4.

**See this code demonstration below:**

public class Main { public static void main(String[] args) { float number1 = 12.4; float number2 = 2.2; float result = 0; result = number1 % number2; System.out.println("The result of” + number1 + “ % ” + number1 + “ = " + result); } }

Output: The result of 12.4 % 2.2 = 1.4

## Notable Use cases of Mod Operator

Java mod operator have been used in solving several problems. Some notable uses of it include checking if a number is even or odd, reversing a number, checking if a number is a palindrome or not and separating every digit from a number.

In this section, we will be discussing the logic behind some of the most common applications of mod operator along with some code examples.

#### 1. Extracting particular digits from an integer.

Thanks to various available features in Java, it is extremely easy to extract a character from a string of variable lengths but this is not the case with integers. As an integer is a single value, we cannot separate individual digits from it directly. To do so, there is a way that requires the use of the mod operator in Java.

We can apply the mod 10 with a number and it will return its last value. Mod 100 will return the last 2 digits, mod 1000 will return the last three digits and so on. By applying the same logic multiple times, any digit can be extracted from an integer.

**See this code example below demonstrating the extraction of the second last digit from a number:**

1. public class Main { 2. public static void main(String[] args) { 3. int number = 31342; 4. int result = number % 100; // now result will contain last two digits combined i.e., 42 5. int result2 = (int) result / 10 // dividing the value by 10 will take out the second last digit. 6. System.out.println("The second last digit” + “ = " + result2); 7. } 8. }

Output: The second last digit = 4

#### 2. Checking if a number is even or odd

This is the most common use case of the mod operator. As the even numbers are multiples of 2, you can mod an integer with 2 and if it returns 0 it means it is completely divisible by 2 indicating it is an even number, or else an odd number would return 1 as the remainder. This exact logic can be used with any other pair of numbers to find out if a number is a multiple of another number.

**See this code example below:**

1. public class Main { 2. public static void main(String[] args) { 3. int number = 32134 4. if(number%2 == 0){ 5. System.out.println(number + “is an Even number"); 6. } 7. else { 8. System.out.println(number + “is an Odd number"); 9. } 10. } 11. }

Output: 32134 is an Even number

#### 3. Check Whether a Number is Prime or Not

This is another problem where the mod operator plays a pivotal role. A prime number is a number that is divisible by only two numbers, 1 and with itself. So, if a number is divisible by any other number, it will not be a prime number. We can easily determine if a number is divisible by any other number or not using the mod operator.

**The Java code for checking a prime number is demonstrated below:**

1. public class Main { 2. public static void main(String[] args) { 3. int number = 17; 4. boolean flag = false; 5. for (int i = 2; i <= number / 2; ++i) { 6. if (number % i == 0) { // condition for non prime number 7. flag = true; 8. break; 9. } 10. } 11. 11. 12. if (!flag) 13. System.out.println(number + " is a prime number."); 14. else 15. System.out.println(number + " is not a prime number."); 16. } 17. } Output 17 is a prime number.

In the above-mentioned code, for loop is used to determine if the given integer number is prime or not. It is to be noted that the for loop is ranged from 2 to num/2. This is because a number is not divisible by more than its half value.

Inside the for loop, the if condition is used to check if the number is divisible by any number in the given range using the mod operator. If the number turns out to be divisible, the flag will be set to true and we will break out of the loop resulting in the number is not a prime number. If num is not divisible by any number, the flag value will remain false and the number will be declared as a prime number.

#### 4. Tracking the circular array

Another good use of the mod operator is to keep a record of the index of the next available element in a circular queue. In the simplest implementation of a circular queue for integer values, all the values are kept in a fixed-size array but it is accessed in circular order.

When we enqueue an element into the circular queue, the next available position also has to be calculated right away. The next available position is then calculated by the modulus of the number of items + 1 with the queue total capacity.

**See this code below:**

1. public class Main { 2. public static void main(String[] args) { 3. int queue_size= 10; 4. int[] circularQueue = new int[queue_size]; 5. int itemsAdded = 0; 6. for (int i = 0; i < 1000; i++) { 7. int indexVal = ++itemsadded % queue_size; 8. circularQueue[indexVal] = i; 9. } 10. } 11. }

In this use case, the mod is required to prevent the indexVal from exceeding the boundaries of the array, therefore, preventing the ArrayIndexOutOfBoundsException. When the whole queue will be filled, in this particular code the next item will overwrite the value in the first element and the cycle will keep going on.

## Wrapping it up

That was all about how to use the mod operator in Java. The modulo operator is used to find the remainder of an integer division that cannot be calculated by any other operator. It can also be very handy in numerous real-life or programming logical problems.

From simple things like finding out if a given number is even or odd, to more complex tasks like tracking the next free position in a circular array, the mod operator does it all and if properly used it can be very helpful in solving similar countless problems.

**Also Read: **Understanding Java 9 Modules