In this C++ tutorial, you shall learn about Remainder Assignment operator, its syntax, and how to use this operator, with examples.

C++ Remainder Assignment

In C++, 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).

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.cpp

#include <iostream>
using namespace std;

int main() {
    int x = 9;
    x %= 2;
    cout << "x : " << x << endl;
}

Output

x : 1
Program ended with exit code: 0
ADVERTISEMENT

Conclusion

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