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++
#include <iostream>
using namespace std;
int main( void )
{
int x, y;
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;
}
|
C
#include <stdio.h>
int main( void )
{
int x, y;
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
import java.io.*;
class GFG {
public static void main (String[] args)
{
int x, y;
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);
}
}
|
Python3
x = 0
y = 0
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)
|
C#
using System;
public class GFG{
static public void Main ()
{
int x, y;
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);
}
}
|
Javascript
<script>
var x, y;
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>" );
</script>
|
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++
#include <iostream>
using namespace std;
int main()
{
float x, y;
float result;
x = 2.3;
y = 1.5;
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;
result = x % y;
printf ( "%f" , result);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args)
{
float x, y;
float result;
x = 2.3 ;
y = 1.5 ;
result = x % y;
System.out.println(result);
}
}
|
Python3
x, y = 0 , 0
result = 0
x = 2.3
y = 1.5
result = x % y
print (result)
|
C#
using System;
public class GFG{
static public void Main ()
{
float x, y;
float result;
x = 2.3;
y = 1.5;
result = x % y;
Console.Write(result);
}
}
|
Javascript
<script>
var x, y;
var result;
x = 2.3;
y = 1.5;
result = x % y;
document.write(result + "<br>" );
</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++
#include <iostream>
using namespace std;
int main( void )
{
int x, y;
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;
}
|
C
#include <stdio.h>
int main( void )
{
int x, y;
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)
{
int x, y;
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);
}
}
|
C#
using System;
public class GFG{
static public void Main ()
{
int x, y;
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);
}
}
|
Note: Some compilers may show the result of the expression as 1 and other may show -1. It depends on the compiler.