 # Modulus Operator

Hi Everyone,

I have been doing some self learning for C++. I cant seem to wrap my head around the logic of using the modulus operator to test whether an integer parameter is odd or even.

The code i have got is

``````#include <iostream>
#include <cmath>

bool isEven(int x)
{
return (x % 2) == 0;
}

int main()
{
std::cout << "Enter an Integer: ";
int x{};
std::cin >> x;

if (isEven(x))
std::cout << x << " is Even\n";
else
std::cout << x << " is Odd\n";

return 0;
}
``````

I have no problem with the code, I just don't understand the logic of

``````(x % 2) == 0;

``````

Thanks,

``````(x % 2) == 0;
``````

This first executes x % 2, which will give a result of either 0 or 1. The result is compared with 0 giving a true/false output which is returned to the calling program

``````if ( x % 2 == 0)
{
return true;
}
else
{
return false;
}

``````

It returns the remainder of an integer division, all even integers will have a remainder of 0 if you divide them by two. But it is much easier to check the least significant bit which will always be zero for an even number.

Modulo, modulus arithmetic is sometimes called clock arithmetic.

G

Sometimes this is easier to understand with numeric examples.
We're talking about X, first we need to see X as odd, so let's set X to 5:
5 / 2 = 2 with a remainder of 1

Simple enough, let's set X to 3 to see another odd:
3 / 2 = 1 with a remainder of 1

Notice that both times that X is odd and we divide by 2 we get a remainder of 1. In fact this is always the case with odd numbers, 27 for instance:
27 / 2 = 13 with a remainder of 1 <<<< Again we get a remainder of 1

Now, let's contrast this with even numbers. So let's set X to 4:
4 / 2 = 2 with a remainder of 0 <<<< And here is the difference

Let's set X to 10, another even number:
10 / 2 = 5 with a remainder of 0 <<<< Again, even numbers give us a remainder of 0

To modulate, for your purposes, gives the remainder of a division problem.
X % 2 is the same as asking, What is the remainder left over when I divide X by 2?

When you modulate by 2 you will always get a 1 or a 0 as the answer. A 1 tells you that the number you modulated by is odd and a 0 tells you that it's even.

To extend your understanding and explore modulus more try modulating numbers by 10 and 100 and see what results you get.

While it is true that if % returns 0 that x was even, but it can return -1, 0, or 1.
% is not mathematical modulus at all as it has crazy behaviour for negative inputs. This is an unfortunate historical accident in (many) programming languages.

Why are people adding unnecessary parentheses?

``````bool isEven (int x) { return x%2 == 0 ;}
// perhaps its because you need them for '&':
bool isEven (int x) { return (x&1) == 0 ;}``````

% is the remainder operator which can be negative or zero for a negative numerator.

Whatever, people often assume its a mathematical modulus operator by neglecting the semantics for negative arguments, and its called the modulo operator in many many places and tutorials.

It is called the modulo, not modulus. Modulo is the operation, modulus divisor of the modulo operation.

In C the modulo operator performs integer division, always rounding towards zero, and the remainder always has the sign of the dividend. Thus making this particular implementation of the moduo operator operate identically to a remainder function.

One must always distinguish this in every language they use as it is not always the case.
Here are the results of a simple sketch to test the point:

20 % 6 =
Calculated: 2
Remainder expected 2
20 % -6 =
Calculated: 2
Remainder expected 2
-20 % 6 =
Calculated: -2
Remainder expected -2
-20 % -6 =
Calculated: -2
Remainder expected -2

Beyond this is just semantics, not syntax, and thar be dragons   