0

How can I decide if a number is odd or even with if else?

17th Dec 2020, 3:48 PM
Alex
Alex - avatar
7 Respuestas
+ 12
Share your attempt first. Hint : modulo(%) operator divides 2 numbers and give remainder
17th Dec 2020, 3:50 PM
Arsenic
Arsenic - avatar
+ 3
Martin Taylor yes using bitwise AND is much faster and better way to do this but I would not recommend bitwise operations to a person who do not even know how to use if-else properly.
18th Dec 2020, 1:47 AM
Arsenic
Arsenic - avatar
- 1
Martin Taylor Good point. But today's compilers actually do a great job at identifying and optimizing these kind of things. So to me, using the modulo is actually better, since it makes it clear to the one's reading your code, what it is that you want to achieve. Leave the optimizations to the compiler
17th Dec 2020, 4:45 PM
Anthony Maina
Anthony Maina - avatar
- 3
If (number%2 == 0) { // Even number }else { // Odd number }
17th Dec 2020, 4:03 PM
Dhaval Patoliya
Dhaval Patoliya - avatar