Task

Test whether an integer is even or odd.

There is more than one way to solve this task:

 Use the even and odd predicates, if the language provides them.
 Check the least significant digit. With binary integers, i bitwise-and 1 equals 0 iff i is even, or equals 1 iff i is odd.
 Divide i by 2. The remainder equals 0 iff i is even. The remainder equals +1 or -1 iff i is odd.
 Use modular congruences:
 i ≡ 0 (mod 2) iff i is even.
 i ≡ 1 (mod 2) iff i is odd.


defmodule RC do
  import Integer
 
  def even_or_odd(n) when is_even(n), do: "#{n} is even"
  def even_or_odd(n)                , do: "#{n} is odd"
      # In second "def", the guard clauses of "is_odd(n)" is unnecessary.
 
  # Another definition way
  def even_or_odd2(n) do
    if is_even(n), do: "#{n} is even", else: "#{n} is odd"
  end
end
 
Enum.each(-2..3, fn n -> IO.puts RC.even_or_odd(n) end)