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.

Similar Reads

Syntax of Modulus Operator

If x and y are integers, then the expression:...

Example of Modulo Operator

Below is the C/C++ program to demonstrate the working of the modulo operator:...

Restrictions on the Modulo Operator

...

Modulo Operator for Negative Operands

...

FAQs on Modulo Operator

The modulo operator has few restrictions or limitations on it. The % modulus operator cannot be applied to floating-point numbers i.e. float or double. If you try to use the modulo operator with floating-point constants or variables, the compiler will produce an error....