+ 2

How can we represent an odd or an even number in a code for example a java code

7th Apr 2018, 11:09 PM
Aiman Aqari
Aiman Aqari - avatar
5 Respuestas
+ 4
yes or if you only care about odds if (number % 2 == 1) { // odd }
7th Apr 2018, 11:18 PM
John Wells
John Wells - avatar
+ 3
thank you so much guys
7th Apr 2018, 11:22 PM
Aiman Aqari
Aiman Aqari - avatar
+ 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?
7th Apr 2018, 11:12 PM
D B
D B - avatar
+ 2
so for odd we use else statement ?
7th Apr 2018, 11:14 PM
Aiman Aqari
Aiman Aqari - avatar
+ 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...) }
7th Apr 2018, 11:18 PM
D B
D B - avatar