Modulo Operator (%) in C/C++ with Examples
The modulo operator, denoted by %, is an arithmetic operator.
The modulo division operator produces the remainder of an integer division.
Syntax: If x and y are integers, then the expression:
x % y
produces the remainder when x is divided by y.
Return Value:
- If y completely divides x, the result of the expression is 0.
- If y is not completely divisible by x, then the result will be the remainder in the range [1, x-1].
- If x is 0, then division by zero is a compile-time error.
For example: Consider the following code:
// Program to illustrate the // working of modulo operator #include <stdio.h> int main(void) { // To store two integer values int x, y; // To store the result of // the modulo expression int result; x = 3; y = 4; result = x % y; printf("%d", result); result = y % x; printf("\n%d", result); x = 4; y = 2; result = x % y; printf("\n%d", result); return 0; } |
3 1 0
Restrictions of the modulo operator:
The modulo operator has quite some restrictions or limitations.
- The % 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 a error:
// Program to illustrate the// working of modulo operator#include <stdio.h>intmain(void){// To store two integer valuesfloatx, y;// To store the result of// the modulo expressionfloatresult;x = 2.3;y = 1.5;result = x % y;printf("%d", result);return0;}chevron_rightfilter_noneCompilation Error:
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; ^ prog.c:20:12: warning: format '%d' expects argument of type 'int', but argument 2 has type 'double' [-Wformat=] printf("%d", result); ^ - The sign of the result for modulo operator is machine-dependent for negative operands, as the action takes as a result of underflow or overflow.
// Program to illustrate the// working of the modulo operator#include <stdio.h>intmain(void){// To store two integer valuesintx, y;// To store the result of// the modulo expressionintresult;x = -3;y = 4;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);return0;}chevron_rightfilter_noneOutput:-3 0 -3
Note: Some compilers may show the result of the expression as 1 and other may show -1. It depends on the compiler.
Recommended Posts:
- Program to find remainder without using modulo or % operator
- ios operator() function in C++11 with Examples
- ios operator() function in C++ with Examples
- Arrow operator -> in C/C++ with Examples
- Logical Not ! operator in C with Examples
- ios operator !() function in C++ with Examples
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Operator Overloading '<<' and '>>' operator in a linked list class
- Modulo 10^9+7 (1000000007)
- Compute n! under modulo p
- LCM of N numbers modulo M
- Fibonacci modulo p
- Maximum subarray sum modulo m
- Maximum frequency of a remainder modulo 2i
- Find sum of modulo K of first N natural number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

