Java modulo operator – Java Program on Modulo Operator

Java modulo operator: In the previous article, we have discussed about Java Program on Division Operator

In this article we will see the use of modulus operator in Java.

Java Program on Division Operator

Modulo operator java: Division operator is a binary operator and it is used to divide two numbers and it returns the reminder value. Modulus operator is represented by the % symbol.

Syntax: operand % operand

Example:

Let’s understand it more clearly.

Suppose we have 2 variables a and b.

a=20

b=5

Then we can use modulus operator like this.

result = a % b;  // here 2 variable operands are divided and reminder is returned.

result = 20%5=0

(or)

result = 14 % b; // here one constant operand i.e. 14 and variable operand i.e. ‘b‘ divided and reminder is returned.

result= 14%5=4

Program:

import java.io.*;
 
class Main 
{
    public static void main(String[] args)
    {
        //Declaring variables
        int number1 = 39;
        int number2 = 2;
        //dividing two numbers by using division operator
        int result = number1%number2;
        
        //print results
        System.out.println("By performing modulus operation: " + result);
    }
}
Output:

By performing modulus operation: 1

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Related Java Programs: