Modulo Operator (%) in C/C++ (original) (raw)

Last Updated : 27 May, 2026

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.

#include using namespace std;

int main() {

int a = 10, b = 3;

int result = a % b;

cout << "10 % 3 = " << result << endl;

return 0;

}

`

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

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

C++ `

#include

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;

}

C

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

C++ `

#include 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;

}

C

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

C++ `

#include 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;

}

C

#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;

}

`

**Note: In C and C++, the sign of the result follows the sign of the dividend (left operand).

**Example

Applications

The modulo operator is widely used in programming for mathematical calculations, looping operations, and data processing tasks.