Odd or Even

I recently started to program in C++, and I was given a math problem to solve. I need to know if there's any way I can check if my variable is Odd or Even? Thanks.
Check out the % operator.
There are at least two methods. The frist one is to use operator %. The second one is to use operator &. For example

int x = 3;

if ( x % 2 != 0 ) std::cout << x << " is odd\n";

or

int x = 3;

if ( x & 1 != 0 ) std::cout << x << " is odd\n";
Last edited on
Thanks! Worked for me
Topic archived. No new replies allowed.