0
Program in c++: how to know this number is even or odd
2 Respuestas
+ 6
If reminder is 0 number is even else odd
3%2 = 1 odd
4%2 = 0 even
+ 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");