Read Java Interview Questions and Answers for Beginners and Experienced

Java Modulus operator Example

In this Java examples of Java tutorial, we will study about Java modulus operator with example. We will study what is the use of Java modulus operator.

 


Java modulus operator

Java Modulus operator is used to get the remainder in the modulus. in Simple words, modulus operator return the value left after dividing two numbers. It returns the 0 when both numbers are fully divided. Modulus operator is represent as %.

Java modulus operator   X % Y

  • Here X and Y are two numbers
  • % is the java modulus operator.

 


Java modulus operator Example

Let’s understand Java modulus operator with example.

public class JavaExamples {

public static void main(String[] args) {
//Java modulus operator with example in Java
int firstNumber=20;
int secondNumber=10;
int result=firstNumber % secondNumber;
System.out.print("result is:"+result);

}

}

output

result is:0

Explanation
In above Java modulus operator program, firstNumber and secondNumber are two integer variables. We applied Java modulus operator on these two numbers. We save the result in result variable.

In this Java operators with example, we saw how Java modulus operator works with example.