Syntax of Modulus Operator
If x and y are integers, then the expression:
x % y;
pronounced as “x mod y”. For example, 10 % 2 will be pronounced as ” Ten mod Two”.
Return Value of Modulo Operator
- If y completely divides x, the result of the expression is 0.
- If x is not completely divisible by y, then the result will be the remainder in the range [0, y-1]
- (x % y) < (x / 2) ………if (x >= y)
- (x % y) = x ……… if (x < y)
- If y is 0, then division by zero is a compile-time error.
Modulo Operator (%) in C/C++ with Examples
In C or C++, the modulo operator (also known as the modulus operator), denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division which is also called the modulus of the operation.