0

Program in c++: how to know this number is even or odd

21st Sep 2022, 8:03 AM
Niranjan Kumar Yadav
Niranjan Kumar Yadav - avatar
2 ответов
+ 6
If reminder is 0 number is even else odd 3%2 = 1 odd 4%2 = 0 even
21st Sep 2022, 8:06 AM
A͢J
A͢J - avatar
+ 1
The fastest way to determine even or odd is to examine the lowest bit. If the bit is 0, then the value is even. If the bit is 1, then the value is odd. You can check the lowest bit of an integer by using the Bitwise And operator &, like n&1. if (n&1) puts("odd"); else puts("even");
21st Sep 2022, 11:35 PM
Brian
Brian - avatar