Operador de módulo (%) en C/C++ con ejemplos

El operador módulo , denotado por % , es un operador aritmético . El operador de división módulo produce el resto de una división entera. 

Sintaxis: si x e y son números enteros, entonces la expresión: 

x % y

produce el resto cuando x se divide por y. 

Valor devuelto: 

  • Si y divide completamente a x, el resultado de la expresión es 0.
  • Si x no es completamente divisible por y, entonces el resultado será el resto en el rango [0, y-1]
  • x mod y <x/2 si x>=y y x mod y=x si x<y
  • Si y es 0, entonces la división por cero es un error de tiempo de compilación .

Por ejemplo: Considere el siguiente código:

C++

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

C

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

// Java Program to illustrate the
// working of modulo operator
import 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

Python3

# Python Program to illustrate the
# working of modulo operator
 
# To store two integer values
x = 0
y = 0
 
# To store the result of
# the modulo expression
result = 0
 
x = 3
y = 4
result = x % y
print(result)
 
result = y % x
print(result)
 
x = 4
y = 2
result = x % y
print(result)
 
# This code is contributed by Shubham Singh

C#

// C# Program to illustrate the
// working of modulo operator
using 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

Javascript

<script>
 
// Program to illustrate the
// working of modulo operator
 
// To store two integer values
var x, y;
 
// To store the result of
// the modulo expression
var 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>
Producción

3
1
0

Restricciones del operador módulo: 
El operador módulo tiene bastantes restricciones o limitaciones.

El operador % no se puede aplicar a números de coma flotante, es decir, float o double. Si intenta utilizar el operador de módulo con constantes o variables de punto flotante, el compilador producirá un error:

C++

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

C

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

// Java implementation of the above approach
import 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

Python3

# Program to illustrate the
# working of modulo operator
 
# To store two integer values
x, y = 0, 0
 
# To store the result of
# the modulo expression
result = 0
 
x = 2.3
y = 1.5
result = x % y
print(result)
 
# This code is contribute by Shubham Singh

C#

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

Javascript

<script>
 
// Program to illustrate the
// working of modulo operator
 
// To store two integer values
var x, y;
 
// To store the result of
// the modulo expression
var result;
 
x = 2.3;
y = 1.5;
result = x % y;
document.write(result +"<br>");
 
// This code is contributed by Shubham Singh
</script>

Error de compilación: 

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

El signo del resultado para el operador módulo depende de la máquina para los operandos negativos, ya que la acción se realiza como resultado de un desbordamiento o desbordamiento. 

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

C

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

Java

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

Python3

# Program to illustrate the
# working of modulo operator
 
# To store two integer values
x, y = 0, 0
 
# To store the result of
# the modulo expression
result = 0
 
x = -3
y = 4
result = x % y
print(result)
 
# This code is contribute by AbhishekASLK

C#

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
Producción

-3
0
-3

Nota: algunos compiladores pueden mostrar el resultado de la expresión como 1 y otros pueden mostrar -1. Depende del compilador .

Publicación traducida automáticamente

Artículo escrito por shubhamkumarlhh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *