0
How can I decide if a number is odd or even with if else?
7 Antworten
+ 12
Share your attempt first.
Hint : modulo(%) operator divides 2 numbers and give remainder
+ 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.
- 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
- 3
If (number%2 == 0) {
// Even number
}else {
// Odd number
}