Detect even/odd number

From CodeCodex

Implementations[edit]

Java[edit]

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[edit]

The built-in method of the Integer class

if x.even?
  # 
end
if x.odd?
  #
end

Tcl[edit]

proc even x {expr {($x % 2) == 0}}
proc odd  x {expr {($x % 2) != 0}}