cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
Odd or Even
Odd or Even
May 8, 2013 at 2:05pm UTC
fredrb
(2)
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.
May 8, 2013 at 2:08pm UTC
Peter87
(11175)
Check out the % operator.
May 8, 2013 at 2:19pm UTC
vlad from moscow
(6539)
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
May 8, 2013 at 2:33pm UTC
May 8, 2013 at 2:36pm UTC
fredrb
(2)
Thanks! Worked for me
Topic archived. No new replies allowed.