Modulo Operator (%) in C/C++ with Examples
Last Updated :
11 Oct, 2024
In C or C++, the modulo operator (also known as the modulus operator), denoted by %, is an arithmetic operator. The modulo division operator produces the remainder of an integer division which is also called the modulus of the operation.
Syntax of Modulus Operator
If x and y are integers, then the expression:
x % y;
pronounced as "x mod y". For example, 10 % 2 will be pronounced as " Ten mod Two".
Return Value of Modulo Operator
- 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 % y) < (x / 2) .........if (x >= y)
- (x % y) = x ......... if (x < y)
- If y is 0, then division by zero is a compile-time error.
To understand operators and their applications in algorithms, check out our Complete C++ Course, where you’ll learn how to use various operators and functions effectively in C++.
Example of Modulo Operator
Below is the C/C++ program to demonstrate the working of the modulo operator:
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
C
// 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;
}
Restrictions on the Modulo Operator
The modulo operator has few restrictions or limitations on it. The % modulus 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.
Example 1: C/C++ program to demonstrate the restrictions of the modulo operator.
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
C
// 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;
}
Output
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.
Example 2: C/C++ program to demonstrate the modulo operator for negative operands.
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
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;
}
Note: The return value in this case is compiler dependent.
Similar Reads
ios operator() function in C++ with Examples The operator() method of ios class in C++ is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: operator void*() const; Parameters: This method does not accept any parameter. Return Value: This method returns a null pointer if any error bit is set of this
1 min read
ios operator() function in C++11 with Examples The operator() method of ios class in C++11 is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: explicit operator bool() const; Parameters: This method does not accept any parameter. Return Value: This method returns false if any error bit is set of this
1 min read
Pointer Arithmetics in C with Examples Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
10 min read
norm() function in C++ with Examples The norm() function is defined in the complex header file. This function is used to return the squared magnitude of the complex number z. Syntax: template<class T> T norm (const complex<T>& z); Parameter: z: It represents the given complex number. Return: It returns the squared magni
1 min read
%n in scanf() in C with Example In C, %n is a special format specifier. In the case of printf() function the %n assign the number of characters printed by printf(). When we use the %n specifier in scanf() it will assign the number of characters read by the scanf() function until it occurs. Key points: It is an edit conversion code
2 min read
showbits( ) Function in C with Examples Bitwise operators are operators (just like +, *, &&, etc.) that operate on ints and units at the binary level. This means they look directly at the binary digits or bits of an integer. This all sounds scary, but in truth, bitwise operators are quite easy to use and also quite useful! It is i
4 min read
unsigned specifier (%u) in C with Examples The format specifier is used during input and output. It is a way to tell the compiler what type of data is in a variable during taking input using scanf() or printing using printf(). Some examples are %c, %d, %f, %u, etc. This article focuses on discussing the format specifier for unsigned int %u.
2 min read
Increment and Decrement Operators in C The increment ( ++ ) and decrement ( -- ) operators in C are unary operators for incrementing and decrementing the numeric values by 1, respectively. They are one of the most frequently used operators in programming for looping, array traversal, pointer arithmetic, and many more.Increment Operator i
4 min read
C++ Increment and Decrement Operators Prerequisite: Operators in C++ What is a C++ increment Operator? The C++ increment operator is a unary operator. The symbol used to represent the increment operator is (++). The increment operator increases the value stored by the variable by 1. This operator is used for Numeric values only. There a
4 min read
Left Shift and Right Shift Operators in C/C++ In C/C++, left shift (<<) and right shift (>>) operators are binary bitwise operators that are used to shift the bits either left or right of the first operand by the number of positions specified by the second operand allowing efficient data manipulation. In this article, we will learn
6 min read