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

17 Jun, 2022
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 [1, x-1].
If y is 0, then division by zero is a compile-time error.
For example: Consider the following code:
C++
C
Java
Python3
C#
Javascript
// Program to illustrate the// working of modulo operator#include <iostream>using namespace std; 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;    cout << result << endl;     result = y % x;    cout << result << endl;     x = 4;    y = 2;    result = x % y;    cout<<result;     return 0;} // This code is contributed by Mayank Tyagi
// 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;}
// Java Program to illustrate the// working of modulo operatorimport java.io.*; class GFG {   // Driver Code  public static void main (String[] args)  {     // 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;    System.out.println(result);     result = y % x;    System.out.println(result);     x = 4;    y = 2;    result = x % y;    System.out.println(result);  }} // This code is contributed by Shubham Singh
# Python Program to illustrate the# working of modulo operator # To store two integer valuesx = 0y = 0 # To store the result of# the modulo expressionresult = 0 x = 3y = 4result = x % yprint(result) result = y % xprint(result) x = 4y = 2result = x % yprint(result) # This code is contributed by Shubham Singh
// C# Program to illustrate the// working of modulo operatorusing System;public class GFG{   // Driver Code  static public void Main ()  {     // 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;    Console.WriteLine(result);     result = y % x;    Console.WriteLine(result);     x = 4;    y = 2;    result = x % y;    Console.WriteLine(result);  }} // This code is contributed by Shubham Singh
<script> // Program to illustrate the// working of modulo operator // To store two integer valuesvar x, y; // To store the result of// the modulo expressionvar result; x = 3;y = 4;result = x % y;document.write(result +"<br>"); result = y % x;document.write(result +"<br>"); x = 4;y = 2;result = x % y;document.write(result +"<br>"); // This code is contributed by Shubham Singh</script>
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:
C++
C
Java
Python3
C#
Javascript
// Program to illustrate the// working of modulo operator#include <iostream>using namespace std; int main(){         // To store two integer values    float x, y;     // To store the result of    // the modulo expression    float result;     x = 2.3;    y = 1.5;    result = x % y;    cout << result;     return 0;} // This code is contributed by Harshit Srivastava
// Program to illustrate the// working of modulo operator #include <stdio.h> int main(void){     // To store two integer values    float x, y;     // To store the result of    // the modulo expression    float result;     x = 2.3;    y = 1.5;    result = x % y;    printf("%f", result);     return 0;}
// Java implementation of the above approachimport java.io.*;import java.util.*; class GFG {     public static void main (String[] args)    {                 // To store two integer values        float x, y;             // To store the result of        // the modulo expression        float result;             x = 2.3;        y = 1.5;        result = x % y;        System.out.println(result);    }} // This code is contributed by Shubham Singh
# Program to illustrate the# working of modulo operator # To store two integer valuesx, y = 0, 0 # To store the result of# the modulo expressionresult = 0 x = 2.3y = 1.5result = x % yprint(result) # This code is contribute by Shubham Singh
using System; public class GFG{     static public void Main ()    {                 // To store two integer values        float x, y;             // To store the result of        // the modulo expression        float result;             x = 2.3;        y = 1.5;        result = x % y;        Console.Write(result);    }} // This code is contributed by Shubham Singh
<script> // Program to illustrate the// working of modulo operator // To store two integer valuesvar x, y; // To store the result of// the modulo expressionvar result; x = 2.3;y = 1.5;result = x % y;document.write(result +"<br>"); // This code is contributed by Shubham Singh</script>
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;
                ^           
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. 
C++
C
Java
C#
// Program to illustrate the// working of the modulo operator#include <iostream>using namespace std; 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;    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
// Program to illustrate the// working of the 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);     x = 4;    y = -2;    result = x % y;    printf("\n%d", result);     x = -3;    y = -4;    result = x % y;    printf("\n%d", result);     return 0;}
import java.io.*; class GFG {    public static void main (String[] args)    {        // 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;        System.out.println(result);         x = 4;        y = -2;        result = x % y;        System.out.println(result);         x = -3;        y = -4;        result = x % y;        System.out.println(result);    }} // This code is contributed by sarajadhav12052009
using System; public class GFG{     static public void Main ()    {          // 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;        Console.WriteLine(result);         x = 4;        y = -2;        result = x % y;        Console.WriteLine(result);         x = -3;        y = -4;        result = x % y;        Console.WriteLine(result);    }} // This code is contributed by sarajadhav12052009
-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.
shubhamkumarlhh
mayanktyagi1709
srivastavaharshit848
alexandrebinninger
SHUBHAMSINGH10
avtarkumar719
harendrakumar123
sarajadhav12052009
C-Operators
cpp-operator
C Language
C++
Mathematical
Programming Language
cpp-operator
Mathematical
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Multidimensional Arrays in C / C++
Function Pointer in C
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
Vector in C++ STL
Initialize a vector in C++ (7 different ways)
Templates in C++ with Examples
Operator Overloading in C++
Inheritance in C++