In this part, our attention will be directed towards the modulus operator.
What is the modulus operator in C and C++?
The modulus operator, represented by the percentage symbol ( % ), is a common feature in multiple programming languages. This operator, found within the realm of arithmetic operators, calculates the remainder of a division operation. If the remainder is 0, it indicates that the number is evenly divisible by the divisor.
Syntax:
rem = a % b
In the given Syntax, a and b represent two integer values, and the % (Percent) symbol functions as a modulus operator, dividing a by b and providing the remainder as the result.
Return Possibilities of the Modulus Operator
Following are the possibilities when the first number is divided by the second number to return only a remainder value.
- If the variable a is completely divisible by the second number (b), it returns zero (0), or the remainder becomes 0.
- If the variable a is not completely divisible by the second number (b), it returns a numeric value in the range [1, a - 1]. Or we can say it returns the remainder to a non-zero integer value.
- If the first number (a) is non-zero and the second number is 0, it gives an error at compile time.
How does Modulus Operator work in C/C++?
The modulus operator operates on two operands provided by the user. It divides the first number by the second number and calculates the remainder. Let's explore an example to demonstrate how the modulus operator functions.
When we use the modulus operator on 8 and 5, represented as 8 % 5, it gives back the remainder 3. This is because when 8 is divided by 5, the quotient is 1 and the remainder is 3.
Likewise, performing the operation 7 modulo 2 results in 1 remaining as the remainder. This is due to the division of 7 by 2 yielding a quotient of 3 and a remainder of 1.
Create a C program that demonstrates the functionality of the Modulus Operator.
Mode.c
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
int res; // store the resultant of modulus expression
a = 5;
b = 2;
res = a % b; // define modulus expression
printf(" Modulus returns a remainder: %d", res);
res = b % a; // define modulus expression
printf(" \n Modulus returns a remainder: %d", res);
a = 10;
b = 5;
res = a % b; // define modulus expression
printf(" \n Modulus returns a remainder: %d", res);
getch();
}
Output:
Modulus returns a remainder: 1
Modulus returns a remainder: 2
Modulus returns a remainder: 0
Note: When we divide a float number by another number, it returns the compiled timer error as an invalid operand. Hence, we can say that it does not work with float number.
Program to implement the modulus operator in C++.
Mode3.cpp
#include<iostream>
using namespace std;
int main(void)
{
int a, b;
int res; // store the resultant of modulus expression
a = 5;
b = 2;
res = a % b; // modulus expression
cout <<" Modulus returns a remainder: " << res;
res = b % a; // modulus expression
cout <<"\n Modulus returns a remainder: " << res;
a = 10;
b = 5;
res = a % b; // modulus expression
cout <<"\n Modulus returns a remainder: " << res;
return 0;
}
Output:
Modulus returns a remainder: 1
Modulus returns a remainder: 2
Modulus returns a remainder: 0
Modulus Operator in C++
Mode4.cpp
#include<iostream>
using namespace std;
int main(void)
{
int a, b;
int res; // store the result of modulus expression
a = -5;
b = 2;
res = a % b; // modulus expression
cout <<" Modulus returns a remainder: " << res;
a = 17;
b = -3;
res = a % b; // modulus expression
cout <<"\n Modulus returns a remainder: " << res;
a = -26;
b = -5;
res = a % b; // modulus expression
cout <<"\n Modulus returns a remainder: " << res;
return 0;
}
Output:
Modulus returns a remainder: -1
Modulus returns a remainder: 2
Modulus returns a remainder: -1
Chaining of Modulus Operator
We have the option to utilize the Chaining of Modulus Operator to conduct modular division on multiple operands within a single instruction. Below is the pseudo-code demonstrating the process of chaining the modulus operator.
res = operand1 % operand2 % operand3 % .... % operand_n
Let's explore the concept of Chaining the Modulus Operator to involve more than two operands.
Mode5.cpp
#include <iostream>
using namespace std;
int main()
{
// Use of modulus operator in C++
int x = 14;
int y = 6;
int z = 3;
int modulo = x % y % z; // x % y returns 2, and 2 % z returns 2
cout << "Modulus is : "<< modulo;
}
Output:
Modulus is: 2