+ 2
How can we represent an odd or an even number in a code for example a java code
5 Antworten
+ 4
yes or if you only care about odds
if (number % 2 == 1) {
// odd
}
+ 3
thank you so much guys
+ 2
All numbers divisible by two without remainder are even, by defenition. So:
if (number % 2 == 0) {
//even!
}
Was that what you were asking for?
+ 2
so for odd we use else statement ?
+ 2
Exactly!
One can negotiate about 0 and non-integers (like 5.6), they are neither odd nor even. Maybe they are oden?
You could also do:
if (number % 2 == 1) {
//odd!
//(at least not even...)
}