Java/J2EE and SOA (324 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (323 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is Modulus in Java and how does it work?

Published on Sep 10,2019 1.3K Views
24 / 72 Blog from Java Core Concepts

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

I am sure you would be familiar with the term Modulus. It is also one of the most commonly asked interview questions for C, C++, Java or Python. In this article, I am going to brush up the definition of modulus to you followed by the implementation process through Java.

Have a look at the agenda of this article:

Let’s begin!

What is a Modulus operator in Java?

The modulus operator returns the remainder of the two numbers after division. If you are provided with two numbers, say, X and Y, X is the dividend and Y is the divisor, X mod Y is there a remainder of the division of X by Y.

Let me make you guys familiar with the technical representation of this operator.

Modulus in Java: Syntax

X%Y

Where X is the dividend and Y is divisor.

So simple, isn’t it?

Now that you are well acquainted with the syntax, I am going to use it in the Java program.

Example of Modulus in Java

class ModulusOperator{
public static void main(String[] args){
int num1,num2,result;
num1=26;
num2=15;
System.out.println("num1=26; num2=15");
result=num1%num2;
System.out.println("The result after modulus operation is : "+result);
}
}

Output:

num1=26 num2=15

The result after modulus operation is: 11

Here, you can see how easy it is to implement this concept in the Java program.

You can also use the modulus operator in finding whether a number is even or odd. Let’s see how!

package com.nullbeans.basics; 
public class Main {
public static void main(String[] args) {
int userInput = 32232;
isEvenNumber(userInput);
}
public static boolean isEvenNumber(int userInput){
if(userInput%2 == 0){
System.out.println("Input is an even number");
return true;
}else {
System.out.println("Input is an odd number");
return false;
}
}
}

Hence, you can see that the modulus operator can be used in many cases. This Modulus in Java can also be used to check whether a number is a prime number or not, or applications that calculated the remaining sum of amounts or likewise.

With this, we come to the end of this article on “Modulus in Java”. I hope you guys are clear with the topics shared and I believe it won’t leave you in any ambiguity now. Keep reading, keep exploring!

Also check out Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.