Java Remainder Assignment

In Java, Remainder Assignment Operator is used to find the division of the variable (left operand) by a value (right operand) and assign the resulting remainder of this division operation to the variable (left operand).

In this tutorial, we will learn how to use Remainder Assignment operator in Java, with examples.

The syntax to find the product of a value 2 with variable x and assign the result to x using Remainder Assignment Operator is

x %= 2

Example

In the following example, we take a variable x with an initial value of 9, divide it with a value of 2 and assign the remainder of this division back to x, using Remainder Assignment Operator.

Main.java

public class Main {
    public static void main(String[] args) {
        int x = 9;
        //remainder assignment
        x %= 2;
        System.out.println("x : " + x);
    }
}

Output

x : 1
ADVERTISEMENT

Conclusion

In this Java Tutorial, we learned about Remainder Assignment Operator in Java, with examples.