Restrictions on the 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.

Example 1: C/C++ program to demonstrate the restrictions of the modulo operator.

C++




// C++ Program to demonstrate the restrictions of modulo
// operator
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
    float x, y;
 
    x = 2.3;
    y = 1.5;
 
    // modulo for floating point values
    result = x % y;
    cout << result;
 
    return 0;
}
 
// This code is contributed by Harshit Srivastava


C




// C Program to illustrate the working of modulo operator
#include <stdio.h>
 
int main(void)
{
    float x, y;
 
    float result;
 
    x = 2.3;
    y = 1.5;
 
    // modulo for floating point values
    result = x % y;
    printf("%f", result);
 
    return 0;
}


Output

Compilation Error in C code :- prog.c: In function 'main':
prog.c:19:16: error:
invalid operands to binary % (have 'float' and 'float')
result = x % y;
^

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