Skip to content
Related Articles
Open in App
Not now

Related Articles

Modulo Operator (%) in C/C++ with Examples

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 06 Jan, 2023
Improve Article
Save Article

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 x is not completely divisible by y, then the result will be the remainder in the range [0, y-1]
  • x mod y <x/2 if x>=y and x mod y=x if x<y
  • If y is 0, then division by zero is a compile-time error.

Below is the C/C++ program to demonstrate the working of the modulo operator:

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

C++




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

Output

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

Below is the C/C++ program to demonstrate the restrictions of the modulo operator:

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

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

Compilation 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;
                ^           

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. 

Below is the C/C++ program to demonstrate the modulo operator for negative operands:

C




// 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;
}

C++




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

Output

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!