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.


 
% Bitwise and
is_even_bitwise(I) = cond(I /\ 1 == 0, true, false).
 
% Modulo
is_even_mod(I) = cond(I mod 2 == 0, true, false).
 
% Remainder
is_even_rem(I) = cond(I rem 2 == 0, true, false).
 
yes_or_no(B) = YN =>
    B = true, YN = "Yes";
    B = false, YN = "No".
 
main :-
    foreach (I in 2..3)
        printf("%d is even? %s\n", I, yes_or_no(is_even_bitwise(I))),
        printf("%d is even? %s\n", I, yes_or_no(is_even_mod(I))),
        printf("%d is even? %s\n", I, yes_or_no(is_even_rem(I)))
    end.
 