Using C++ Relational Ops to Check if a Number is Even

  • Context: C/C++ 
  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around using C++ relational operations to determine if a number is even by checking the result of a modulus operation. Participants explore the behavior of the modulus operator with both positive and negative integers, as well as the implications of using it in different contexts.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant suggests using the expression userNum % 2 == 0 to check if a number is even, questioning why the equality to 0 is necessary.
  • Another participant explains that x % 2 gives the remainder when x is divided by 2, noting that even numbers yield a remainder of 0.
  • A participant questions whether the only possible outcomes of x % 2 are 0 or 1.
  • Another participant confirms that the remainder when dividing by 2 can only be 0 or 1, but adds a caveat regarding negative numbers and the behavior of the modulus operator in C++.
  • A further elaboration indicates that for negative numbers, the result of x % 2 can be negative, specifically mentioning that (-3) % 2 results in -1, and discusses the implications of using the modulus operator with signed integers.
  • There is a note about the historical context of the C++ standard regarding the sign of the result of the modulus operation, particularly before C++11.

Areas of Agreement / Disagreement

Participants generally agree on the behavior of the modulus operation for nonnegative integers, but there is disagreement regarding its behavior with negative integers, leading to a nuanced discussion about the implications of using the modulus operator in different contexts.

Contextual Notes

There are limitations regarding the assumptions made about the sign of numbers when using the modulus operator, as well as the potential for confusion between the modulus operation and the division remainder concept in C++.

ineedhelpnow
Messages
649
Reaction score
0
Write an expression that will print "Even" if the value of userNum is an even number.

Code:
#include <iostream>
using namespace std;

int main() {
   int userNum = 0;

   userNum = 6;

   if (<STUDENT CODE>) { 
      cout << "Even" << endl; 
   }
   else {
      cout << "Odd" << endl;
   }

   return 0;
}

the <STUDENT CODE> is
Code:
userNum %2==0

i understand the %2 part but why equal to 0? how does the ==0 part detect that its an even number?
 
Technology news on Phys.org
x %2 gives the remainder of x when divided by 2.

for odd numebr x %2 = 1 and for even number x % 2 = 0

so this check is made
 
will it always be either x%2==0 or x%2==1?
 
ineedhelpnow said:
will it always be either x%2==0 or x%2==1?

Yes it shall be as when we divide by 2 remainder can be 0 or 1 and no other value
 
kaliprasad said:
Yes it shall be as when we divide by 2 remainder can be 0 or 1 and no other value

As long as userNum is nonnegative. If it is negative then the result will be 0 or -1, as defined by the C++ standard (which itself follows the Fortran definition of the modulo operation). In short, % is not "modulo", it's "division remainder", which may be negative as integer division always rounds towards zero, not negative infinity.

For instance, let $q$ be the quotient of $-3$ divided by $2$, which is $q = -1$ (round towards zero). Then $r = (-3) ~ \% ~ 2$ is defined as the unique integer satisfying $2q + r = -3$, that is, $r =-1$. So (-3) % 2 = -1.

That is also why I recommend only using % when working with unsigned integer types. It's very easy for people to mistake it for a modulo operation and introduce major security issues that can become very hard to find.

(well, to be specific up until the recent C++11, the sign of (-3) % 2 was implementation-defined, but essentially every processor on the planet will have returned -1; now, it must return -1)​
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 28 ·
Replies
28
Views
30K
  • · Replies 1 ·
Replies
1
Views
1K