Java modulo operator
In this post, you will learn about the modulo operator in Java programming language. Modulo is used in many mathematical areas. The real life example of modulo is sharing of a pizza slices in a group. Suppose, there is a pizza with 10 slices that we have to share among a group of three. How many slices are left after sharing? For this solution, we can use the modulo, i.e., 10 mod 3 = 1. It means 1 slice remains after equally sharing.
About the Modulo Operator
The modulo (or "modulus" or "mod") is the remainder after dividing one number by another. The modulo operator is denoted by %.
Syntax-a%b
It requires two operands. It divides the left-hand operand by the right-hand operand and gives the remainder.
Example-
5%2 = 1
3%2 = 1
9%3 = 0
12%7 = 5
-12%-5 = -2
Uses of Modulo Operator
- The modulo operator is used to find out if a given number is odd or even.
- The modulo operator is used to calculate the checksums of serial numbers.
- It is used to keep track of the index of the next free spot in a circular array.
- It is applied in Euclidean algorithm for greatest common factor calculation.
- It is applied in many scientific areas, like, cryptography, computer algebra.
Java Modulo Operator Program1
public class ModuloOperatorProgram
{
public static void main(String args[])
{
int x = 5;
int y = 2;
int mod = x%y;
System.out.println("Result of modulus operation : "+mod);
}
}
Output of the above code:
Result of modulus operation : 1
Java Modulo Operator Program2 (reverse a number)
In the given Java program, we have used the modulo operator to reverse a number.
public class ModuloOperatorProgram
{
public static void main(String args[])
{
int num = 532198;
int rev = 0;
while(num != 0)
{
// getting remainder using
// modulo operator
int remainder = num % 10;
rev = rev * 10 + remainder;
num = num/10;
}
System.out.println("Reverse of the given number : " + rev);
}
}
Output of the above code:
Reverse of the given number : 891235
Java Modulo Operator Program3 (find even number)
In the given Java program, we have used the modulo operator to find the even numbers in a given array.
public class ModuloOperatorProgram
{
public static void main(String args[])
{
int[] arr = {6, 3, 1, 2, 9, 8, 10, 7};
for(int i=0; i<arr.length; i++)
{
int num = arr[i];
if((num % 2) == 0)
System.out.println(num + " is even.");
else
System.out.println(num + " is odd.");
}
}
}
Output of the above code:
6 is even.
3 is odd.
1 is odd.
2 is even.
9 is odd.
8 is even.
10 is even.
7 is odd.
Java Modulo Operator Program4 (repeat nth time)
In the given Java program, we have used the modulo operator to repeat something given number of time.
public class ModuloOperatorProgram
{
public static void main(String args[])
{
for(int i = 1; i <= 20; i++)
{
if(i % 5 == 0)
System.out.println("Hello ");
}
}
}
Output of the above code:
Hello
Hello
Hello
Hello
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number