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

  • Thread starter Thread starter ineedhelpnow
  • Start date Start date
  • Tags Tags
    C++
AI Thread Summary
To determine if a number is even in C++, the expression userNum % 2 == 0 is used. The modulo operator (%) calculates the remainder of userNum when divided by 2. If the result is 0, the number is even; if it is 1, the number is odd. This behavior holds true for nonnegative integers, where the remainder can only be 0 or 1. However, for negative integers, the result of the modulo operation can be negative, which is defined by the C++ standard. This can lead to confusion, as the modulo operation is not strictly the same as the mathematical definition of modulo. It is advisable to use the modulo operator with unsigned integers to avoid potential issues.
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)​
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top