Detect even/odd number
From CodeCodex
Contents |
Implementations
Java
Use the modulus operator. For Other interesting modulus operations, see http://mindprod.com/jgloss/modulus.html
if (x % 2 == 0) {
// even
}
if (x % 2 != 0) {
// odd
}
... or binary AND operator...
if (( x & 1 ) == 0) {
// even
}
if (( x & 1 ) != 0) {
// odd
}
Ruby
The built-in method of the Integer class
if x.even? # end if x.odd? # end
Tcl
proc even x {expr {($x % 2) == 0}}
proc odd x {expr {($x % 2) != 0}}