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.


 
MODULE EvenOdd;
IMPORT StdLog,Args,Strings;
 
PROCEDURE BitwiseOdd(i: INTEGER): BOOLEAN;
BEGIN
	RETURN 0 IN BITS(i)
END BitwiseOdd;
 
PROCEDURE Odd(i: INTEGER): BOOLEAN;
BEGIN
	RETURN (i MOD 2) # 0
END Odd;
 
PROCEDURE CongruenceOdd(i: INTEGER): BOOLEAN;
BEGIN
	RETURN ((i -1) MOD 2) = 0
END CongruenceOdd;
 
PROCEDURE Do*;
VAR
	p: Args.Params;
	i,done,x: INTEGER;
BEGIN
	Args.Get(p);
	StdLog.String("Builtin function: ");StdLog.Ln;i := 0;
	WHILE i < p.argc DO
		Strings.StringToInt(p.args[i],x,done);
		StdLog.String(p.args[i] + " is:> "); 
		IF ODD(x) THEN StdLog.String("odd") ELSE StdLog.String("even") END;
		StdLog.Ln;INC(i)
	END;
	StdLog.String("Bitwise: ");StdLog.Ln;i:= 0;
	WHILE i < p.argc DO
		Strings.StringToInt(p.args[i],x,done);
		StdLog.String(p.args[i] + " is:> "); 
		IF BitwiseOdd(x) THEN StdLog.String("odd") ELSE StdLog.String("even") END;
		StdLog.Ln;INC(i)
	END;
	StdLog.String("Module: ");StdLog.Ln;i := 0;
	WHILE i < p.argc DO
		Strings.StringToInt(p.args[i],x,done);
		StdLog.String(p.args[i] + " is:> "); 
		IF Odd(x) THEN StdLog.String("odd") ELSE StdLog.String("even") END;
		StdLog.Ln;INC(i)
	END;
	StdLog.String("Congruences: ");StdLog.Ln;i := 0;
	WHILE i < p.argc DO
		Strings.StringToInt(p.args[i],x,done);
		StdLog.String(p.args[i] + " is:> "); 
		IF CongruenceOdd(x) THEN StdLog.String("odd") ELSE StdLog.String("even") END;
		StdLog.Ln;INC(i)
	END;
END Do;
 