How to resolve the algorithm Even or odd step by step in the Picat programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Even or odd step by step in the Picat programming language

Table of Contents

Problem Statement

Test whether an integer is even or odd. There is more than one way to solve this task:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Even or odd step by step in the Picat programming language

Source code in the picat programming language

% 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.

  

You may also check:How to resolve the algorithm Sleep step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Sort three variables step by step in the OCaml programming language
You may also check:How to resolve the algorithm Hamming numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Multiplicative order step by step in the Wren programming language
You may also check:How to resolve the algorithm Sorting algorithms/Permutation sort step by step in the Ursala programming language