Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
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!
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.
X%Y
Where X is the dividend and Y is divisor.
Now that you are well acquainted with the syntax, I am going to use it in the Java program.
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!
If you’re just beginning, then watch at this Java Tutorial to Understand the Fundamental Java Concepts.
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
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co