The modulo operator (%) is an arithmetic operator used in C and C++ to find the remainder after dividing one integer by another. It returns the leftover value of an integer division operation.
- Used to calculate the remainder of division operations between integers.
- Commonly used in loops, checking even/odd numbers, circular indexing, and hashing.
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 3;
int result = a % b;
cout << "10 % 3 = " << result << endl;
return 0;
}
Output
10 % 3 = 1
Syntax
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".
Example
If 10 % 3 is calculated, the result is 1 because dividing 10 by 3 leaves a remainder of 1. Similarly, 8 % 2 returns 0, which indicates that 8 is an even number.
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++.
#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;
}
#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
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.
#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;
}
#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.
#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;
}
#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: In C and C++, the sign of the result follows the sign of the dividend (left operand).
Example
-10 % 3 = -110 % -3 = 1
Applications
The modulo operator is widely used in programming for mathematical calculations, looping operations, and data processing tasks.
- Used to check whether a number is even or odd.
- Used in cyclic operations like circular queues and clock calculations.
- Used in hashing algorithms and array indexing.
- Used in competitive programming for handling large number operations.