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

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (17 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (339 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Modulus in Java and how does it work?

Last updated on Mar 29,2022 94.1K Views

24 / 72 Blog from Java Core Concepts

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 Online Course 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. Join our Java Training in Jalandhar

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 25th June,2022

25th June

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
Send OTP
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.

image not found!
image not found!

What is Modulus in Java and how does it work?

edureka.co