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
SUMMARY

The discussion focuses on using C++ relational operations to determine if a number is even by employing the expression userNum % 2 == 0. The modulo operator (%) returns the remainder of a division, and for even numbers, this remainder is always 0. The conversation highlights that while the expression works for nonnegative integers, the behavior of the modulo operation with negative integers can yield unexpected results, specifically that (-3) % 2 equals -1. It is emphasized that using the modulo operator with unsigned integers is recommended to avoid security issues.

PREREQUISITES
  • Understanding of C++ syntax and basic programming concepts
  • Familiarity with the modulo operator (%) and its behavior
  • Knowledge of relational operators in C++
  • Awareness of integer types and their properties in C++
NEXT STEPS
  • Study the C++ modulo operator and its implications with signed vs. unsigned integers
  • Learn about C++ relational operators and their usage in conditional statements
  • Explore integer division and remainder behavior in C++
  • Investigate best practices for handling negative numbers in programming
USEFUL FOR

Programmers, particularly those working with C++, students learning about conditional logic, and developers interested in understanding integer operations and their implications in code.

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 15 ·
Replies
15
Views
4K
  • · Replies 4 ·
Replies
4
Views
5K
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
13K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 28 ·
Replies
28
Views
30K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
3
Views
1K