How to Use Modulus in C++
What is Modulo Operator?
“The modulo operator, a mathematical procedure, returns the result of the division operation.” In most programming languages, it is denoted by the percentage sign (%). By dividing two numbers, the modulo operator is used. It generates the remainder as its output.
As an illustration, if we divide ten by 3 and apply the modulo operator, we get
20% 6= 2
The modulo operator is frequently used in programming for various tasks, such as encapsulating an array index to guarantee that it remains within the array's boundaries or determining if a value is odd or even by looking at the remainder of a division by two.
Modulo Operator Characteristics
The modulo operator can be beneficial in a range of mathematical and computing situations because of several significant features. The modulo operator has several key characteristics:
Cyclic
The outcome of the modulo operation is usually in the interval 0 = x% y y, where x represents the dividend and y is the divisor. This indicates that the outcome generated by the modulo operation is cyclic, with an interval equal to the divisor.
Commutative
The modulo function is commutative, which means that with any integers that are positive a and b, (a% b) = (b% a).
Associative
The modulo operator is associative, which means that for any of the positive integers a, b, and c, ((a% b)% c) = (a% (b% c)).
Distributive
For every positive integer a, b, and c, (a + b)% c = ((a% c) + (b% c))% c and (a - b)% c = ((a% c) - (b% c))% c, respectively. This is because the modulo operation is distributive across addition and subtraction.
Transitive
If (a%b) = (c%b), then a = (c+nb) for a particular integer n.
It follows that when a = bq + r for any integer q and 0 = r b, then a% b = r. This means that the modulo operation is linear.
Due to these characteristics, the modulo operator, can address a range of mathematical and computing issues, including looping around an array index and determining the least common multiple of two values.
How the Modulo Operator Works in C++
The modulus operator% is used C++ to determine the residual after dividing two integers. The modulo operator has the following syntax in C++:
dividend% divisor =remainder;
The outcome of dividing the dividend by the divisor in this case is the integer remainder. Dividend and divisor are both integer values.
Example 1
#include <iostream>
int main() {
int a = 20;
int b = 6;
int result = a% b;
std::cout << result << std::endl;
return 0;
}
Output
![How to Use Modulus in C++](https://static.tutorialandexample.com/cpp/how-to-use-modulus-in-cpp1.png)
Explanation
Since 20 divided by 6 = 2, the software will produce 1.
It's vital to remember that, regardless of whether the result is not a whole number, the modulus operation's output in C++ will always be an integer. Or the fractional portion of the result is ignored.
Example 2
It's also important to remember that in both C and C++, the sign of the dividend, not the divisor, determines the value of the result of the modulus operation. Even though the divisor is positive, the outcome will be negative if the dividend is negative. For instance,
Example
#include <iostream>
int main() {
int a = -20;
int b = 6;
int result = a% b;
std::cout << result << std::endl;
return 0;
}
Output
![How to Use Modulus in C++](https://static.tutorialandexample.com/cpp/how-to-use-modulus-in-cpp2.png)
Explanation
Since -20 divided by 6 = -18 with a remainder of -2, this program will output -2.
Using the modulo operator to find odd or even numbers
Example
#include <iostream>
int main()
{
int a=20 ;
int result = a % 2;
if (result == 0)
{
std::cout << a<< " is an even number" << std::endl;
}
else
{
std::cout << a << " is an odd number" << std::endl;
}
return 0;
}
Output
![How to Use Modulus in C++](https://static.tutorialandexample.com/cpp/how-to-use-modulus-in-cpp3.png)
Conclusion
The distributive property, associativity, and compatibility with arithmetic rules are only useful characteristics of modulo operations.
The residual following the division of a number by another is returned by the modulo operator, commonly written as % in programming languages. For instance, seven divided by 3 equals 2, leaving a remnant of 1, so 7% three equals 1.
When performing a modulo operation, the outcome is always (exclusively) between 0 and the divisor. Therefore, in the preceding example, the outcome of 7% three will always be between 0 and 2 (exclusive).