What is Modulus in Java and how does it work

Related Courses

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Next Batch : Invalid Date

Modulus in Java

Most of you must have heard about the Modulus term. You will find a question related to it in your interview for C, C++, Python, or Java always. In this artifact, we will discuss the modulus and then will implement it through a program in Java. The topics we will cover in this article are what a modulus operator is, its syntax, and an example of the Modulus. So, let us begin our article. And you can contact us for your Java training. Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. 

Modulus Operator

This operator returns the remainder when a dividend is divided by a divisor, and we term it as X mod Y. Let us have a technical syntax of this modulus operator.

X%Y 

Here X is the dividend and the divisor is Y. And you can use it for finding the remainder when a dividend is divided by a divisor.

Hope now you have the complete details of the Modulus operator, and now let us use it in a Java program.

Program sample

class Main{
public static void main(String[] args){
int a,b,res;
a=30;
b=10;
System.out.println(a+" "+b);
res=a%b;
System.out.println("The modulus is : "+res);
}
}	
Output:
30 10
The modulus is: 0

And, hence, it is quite easy to implement the modulus operator in Java.

We can also make use of the modulus operator in java to find whether a given number is divisible by 3. Let us have a look below:

public class Main 
 {
public static void main(String[] args) 
{
int a = 453;
isDivisibleBythree(a);
}
public static boolean isDivisibleBythree(int a)
{
if(a%3 == 0)
{
System.out.println("Number is divisible by 3");
}
return true;
}
}

Hence, the modulus operator is being used in various cases. And it can be used for finding each digit of a number, as well as in finding the prime numbers. There is a long list of applications of the modulus operator. And that is why the modulus operator is a very useful operator in Java or any programming language.

Let's have a look at the program that uses the modulus operator to find out each digit of a given number. The trick is, we divide the number by 10, and store the remainders in a dynamic array in Java. 

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		System.out.println("Enter Number:");
		Scanner s =new Scanner(System.in);
		int num=s.nextInt();
		ArrayList<Integer> myList = new ArrayList<>();
		while(num!=0)
		{
myList.add(num%10);
		    num=num/10;
		}
		System.out.println(myList);
	
	}
}
Let's have another program in which we reverse a number using a modulus operator. Remember we are using here ArrayList as we need here a dynamic array for storing the digits of a number. The program code is as below:
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
		System.out.println("Enter Number:");
		Scanner s =new Scanner(System.in);
		int num=s.nextInt();
		ArrayList<Integer> myList = new ArrayList<>();
		while(num!=0)
		{
myList.add(num%10);
		    num=num/10;
		}
		System.out.println(myList);
		System.out.println("The Reverse of the number is:");
		for(Integer p: myList)
		{
		  System.out.print(p);
		}
	
	}

You need to know that there is a very deep implementation of the Data structure in Java. And if you want to learn all that, you can contact us anytime. And apart from these, various web technologies are implemented using Java. There is J2EE, J2ME, and J2SE. And java is used extensively in Android as well. And we teach all that. You can find an online form on our site, and send us your queries. Do not forget to add your phone number and email address, so that we can reply to you. And feel free to contact us anytime. 

And this we have reached the end of the article. Hope you now know the modulus operator.

Naresh I Technologies is the number one computer training institute in Hyderabad and among the top five computer training institutes in India. Contact us anytime for your Java training. You can also opt for Java online training, and from any part of the world. And a big package is waiting for you. And all is yours for a nominal fee affordable for all with any range of budget. Let us have a look at what you will get with this Java training package:

  • You need to pay a nominal fee.

  • You can choose any Java certification, as per your skills and interest.

  • You have the option to select from online and classroom training.

  • A chance to study at one of the best Java training institutes in India 

  • We provide Java training in Hyderabad and USA, and no matter in which part of the world you are, you can contact us.

  • Naresh I technologies cater to one of the best Java training in India.

  • And a lot more is waiting for you.

Contact us anytime for your complete Java online training.