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.
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.
To understand operators and their applications in algorithms, check out our Complete C++ Course, where you’ll learn how to use various operators and functions effectively in C++.
Example of Modulo Operator
Below is the C/C++ program to demonstrate the working of the modulo operator:
// C++ Program to demonstrate the working of modulo operator
#include <iostream>
using namespace std;
// Driver code
int main(void)
{
int x, y;
int result;
x = 3;
y = 4;
// using modulo operator
result = x % y;
cout << result << endl;
result = y % x;
cout << result << endl;
// for different values
x = 4;
y = 2;
result = x % y;
cout << result;
return 0;
}
// This code is contributed by Mayank Tyagi
// C++ Program to demonstrate the working of modulo operator
using namespace std;
// Driver code
int main(void)
{
int x, y;
int result;
x = 3;
y = 4;
// using modulo operator
result = x % y;
cout << result << endl;
result = y % x;
cout << result << endl;
// for different values
x = 4;
y = 2;
result = x % y;
cout << result;
return 0;
}
// This code is contributed by Mayank Tyagi
// C Program to illustrate the working of modulo operator
#include <stdio.h>
int main(void)
{
int x, y;
int result;
x = 3;
y = 4;
// using modulo operator
result = x % y;
printf("%d", result);
result = y % x;
printf("\n%d", result);
// for different values
x = 4;
y = 2;
result = x % y;
printf("\n%d", result);
return 0;
}
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++ 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++ Program to demonstrate the restrictions of modulo
// operator
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 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 for Negative Operands
The sign of the result for the modulo operator is machine-dependent for negative operands, as the action takes as a result of underflow or overflow.
Example 2: C/C++ program to demonstrate the modulo operator for negative operands.
// C++ Program to demonstrate the working of the modulo
// operator for negative operands
#include <iostream>
using namespace std;
// Driver code
int main(void)
{
int x, y;
int result;
x = -3;
y = 4;
// modulo for negative operands
result = x % y;
cout << result << endl;
x = 4;
y = -2;
result = x % y;
cout << result << endl;
x = -3;
y = -4;
result = x % y;
cout << result;
return 0;
}
// This code is contributed by Harshit Srivastava
// C++ Program to demonstrate the working of the modulo
// operator for negative operands
using namespace std;
// Driver code
int main(void)
{
int x, y;
int result;
x = -3;
y = 4;
// modulo for negative operands
result = x % y;
cout << result << endl;
x = 4;
y = -2;
result = x % y;
cout << result << endl;
x = -3;
y = -4;
result = x % y;
cout << result;
return 0;
}
// This code is contributed by Harshit Srivastava
// C Program to illustrate the working of the modulo
// operator with negative operands
#include <stdio.h>
int main(void)
{
int x, y;
int result;
x = -3;
y = 4;
// modulo for negative operands
result = x % y;
printf("%d", result);
x = 4;
y = -2;
result = x % y;
printf("\n%d", result);
x = -3;
y = -4;
result = x % y;
printf("\n%d", result);
return 0;
}
Output
-3 0 -3
Note: The return value in this case is compiler dependent.