Even or odd: Difference between revisions

17,168 bytes added ,  2 months ago
m
(Even or odd en Asymptote)
 
(47 intermediate revisions by 29 users not shown)
Line 1:
{{task}} [[Category:Simple]]
 
;Task:
Line 15:
 
=={{header|0815}}==
<langsyntaxhighlight lang="0815">
}:s:|=<:2:x~#:e:=/~%~<:20:~$=<:73:x<:69:~$~$~<:20:~$=^:o:<:65:
x<:76:=$=$~$<:6E:~$<:a:~$^:s:}:o:<:6F:x<:64:x~$~$$<:a:~$^:s:
</syntaxhighlight>
</lang>
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F is_even(i)
R i % 2 == 0
 
F is_odd(i)
R i % 2 == 1</langsyntaxhighlight>
 
=={{header|6502 Assembly}}==
<langsyntaxhighlight lang="6502 assembly">
.lf evenodd6502.lst
.cr 6502
Line 77:
;------------------------------------------------------
.en
</syntaxhighlight>
</lang>
 
=={{header|68000 Assembly}}==
 
===Non-Destructive===
<langsyntaxhighlight lang="68000devpac">BTST D0,#1
BNE isOdd
;else, is even.</langsyntaxhighlight>
 
===Destructive===
 
<langsyntaxhighlight lang="68000devpac">AND.B D0,#1
BNE isOdd
;else, is even.</langsyntaxhighlight>
 
<langsyntaxhighlight lang="68000devpac">ROR.B D0,#1
BCS isOdd
;else, is even.</langsyntaxhighlight>
 
<langsyntaxhighlight lang="68000devpac">ROXR.B D0,#1
BCS isOdd
;else, is even.</langsyntaxhighlight>
 
<langsyntaxhighlight lang="68000devpac">LSR.B D0,#1
BCS isOdd
;else, is even.</langsyntaxhighlight>
 
<langsyntaxhighlight lang="68000devpac">ASR.B D0,#1
BCS isOdd
;else, is even.</langsyntaxhighlight>
 
 
Line 114 ⟶ 115:
The instruction that's doing all the work here is <code>rar</code>, which is a bitwise right rotate
of the accumulator through the carry flag. That leaves the low bit in the carry flag, which will be
set if odd and clear if even.
 
<langsyntaxhighlight lang="8080asm">CMDLIN: equ 80h ; Location of CP/M command line argument
puts: equ 9h ; Syscall to print a string
;;; Check if number given on command line is even or odd
org 100h
lxi h,CMDLIN ; Find length of argument
mov a,m
add l ; Look up last character (digit)
mov l,a
Line 132 ⟶ 133:
jmp 5 ; So print 'even'
even: db 'Even$' ; Strings
odd: db 'Odd$'</langsyntaxhighlight>
 
{{out}}
Line 148 ⟶ 149:
=={{header|8086 Assembly}}==
===Non-Destructive===
<langsyntaxhighlight lang="asm">test ax,1
jne isOdd
;else, is even</langsyntaxhighlight>
 
===Destructive===
<langsyntaxhighlight lang="asm">and ax,1
jne isOdd
;else, is even</langsyntaxhighlight>
 
<langsyntaxhighlight lang="asm">ror ax,1
jc isOdd
;else, is even</langsyntaxhighlight>
 
<langsyntaxhighlight lang="asm">rcr ax,1
jc isOdd
;else, is even</langsyntaxhighlight>
 
<langsyntaxhighlight lang="asm">sar ax,1
jc isOdd
;else, is even</langsyntaxhighlight>
 
<langsyntaxhighlight lang="asm">shr ax,1
jc isOdd
;else, is even</langsyntaxhighlight>
 
The <code>DIV</code> instruction can also work, but using <code>DIV</code> to divide by 2 is a waste of time, since the shift and rotate commands above do it faster.
Line 177 ⟶ 178:
=={{header|8th}}==
The 'mod' method also works, but the bit method is fastest.
<langsyntaxhighlight lang="forth">: odd? \ n -- boolean
dup 1 n:band 1 n:= ;
: even? \ n -- boolean
odd? not ;</langsyntaxhighlight>
 
This could be shortened to:
<langsyntaxhighlight lang="forth">
: even? \ n -- f
1 n:band not ;
: odd? \ n -- f
even? not ;
</syntaxhighlight>
</lang>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B and android arm 64 bits*/
/* program oddEven64.s */
 
/*******************************************/
Line 208 ⟶ 210:
sMessResultEven: .asciz " @ is even (pair) \n"
szCarriageReturn: .asciz "\n"
 
/*********************************/
/* UnInitialized data */
Line 218 ⟶ 220:
/*********************************/
.text
.global main
main: //entry of program
 
mov x0,#5
Line 227 ⟶ 229:
mov x0,#2021
bl testOddEven
100: //standard end of the program
mov x0, #0 //return code
mov x8, #EXIT //request to exit program
svc #0 //perform the system call
 
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResultOdd: .quad sMessResultOdd
Line 266 ⟶ 268:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 273 ⟶ 275:
2021 is odd (impair)
</pre>
 
=={{header|ABAP}}==
<syntaxhighlight lang="abap">
<lang ABAP>
cl_demo_output=>display(
VALUE string_table(
Line 286 ⟶ 289:
)
).
</syntaxhighlight>
</lang>
 
{{out}}
Line 305 ⟶ 308:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC OddByAnd(INT v)
IF (v&1)=0 THEN
Print(" even")
Line 339 ⟶ 342:
 
FOR i=-4 TO 4
DO
PrintF("%I is",i)
OddByAnd(i)
Line 346 ⟶ 349:
PutE()
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Even_or_odd.png Screenshot from Atari 8-bit computer]
Line 362 ⟶ 365:
 
=={{header|Ada}}==
<langsyntaxhighlight lang="ada">-- Ada has bitwise operators in package Interfaces,
-- but they work with Interfaces.Unsigned_*** types only.
-- Use rem or mod for Integer types, and let the compiler
Line 376 ⟶ 379:
Put_Line ("Something went really wrong!");
end if;
end;</langsyntaxhighlight>
 
=={{header|Agda}}==
<syntaxhighlight lang="agda">
<lang Agda>even : ℕ → Bool
module EvenOrOdd where
 
open import Data.Bool using (Bool; false; true)
open import Data.Nat using (ℕ; zero; suc)
 
even : ℕ → Bool
odd : ℕ → Bool
 
Line 386 ⟶ 395:
 
odd zero = false
odd (suc n) = even n</lang>
</syntaxhighlight>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">if (x & 1) {
# x is odd
} else {
# x is even
}</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight lang="algol68"># Algol 68 has a standard operator: ODD which returns TRUE if its integer #
# operand is odd and FALSE if it is even #
# E.g.: #
Line 405 ⟶ 415:
read( ( n ) );
print( ( whole( n, 0 ), " is ", IF ODD n THEN "odd" ELSE "even" FI, newline ) )
</syntaxhighlight>
</lang>
 
=={{header|ALGOL-M}}==
Because ALGOL-M lacks a built-in MOD operator or function and does not support bitwise operations on integers, the test is a bit cumbersome, but gets the job done.
<langsyntaxhighlight lang="algol">
BEGIN
 
Line 424 ⟶ 434:
WRITE(K," IS ", IF EVEN(K) = 1 THEN "EVEN" ELSE "ODD");
 
END</langsyntaxhighlight>
{{out}}
<pre>
Line 433 ⟶ 443:
 
An alternate (but mathematically equivalent) coding, demonstrating the use of a conditional test as part of an assignment statement:
<langsyntaxhighlight lang="algol">
% RETURN 1 IF EVEN, OTHERWISE 0 %
INTEGER FUNCTION EVEN(I);
Line 440 ⟶ 450:
EVEN := (IF I = 2 * (I / 2) THEN 1 ELSE 0);
END;
</syntaxhighlight>
</lang>
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin
% the Algol W standard procedure odd returns true if its integer %
% parameter is odd, false if it is even %
Line 450 ⟶ 460:
write( i, " is ", if odd( i ) then "odd" else "even" )
end for_i
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 460 ⟶ 470:
 
=={{header|AntLang}}==
<langsyntaxhighlight AntLanglang="antlang">odd: {x mod 2}
even: {1 - x mod 2}</langsyntaxhighlight>
 
=={{header|APL}}==
The easiest way is probably to use modulo.
<langsyntaxhighlight lang="apl"> 2|28
0
2|37
1</langsyntaxhighlight>
 
So you can write a user-defined operator.
Line 474 ⟶ 484:
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">set L to {3, 2, 1, 0, -1, -2, -3}
 
set evens to {}
Line 487 ⟶ 497:
end repeat
 
return {even:evens, odd:odds}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight AppleScriptlang="applescript">{even:{2, 0, -2}, odd:{3, 1, -1, -3}}</langsyntaxhighlight>
 
 
Or, packaging reusable functions that can serve as arguments to '''filter''', '''partition''' etc
(deriving '''even''' from mod, and '''odd''' from even):
<langsyntaxhighlight AppleScriptlang="applescript">----------------------- EVEN OR ODD ------------------------
 
-- even :: Int -> Bool
Line 552 ⟶ 562:
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
Line 560 ⟶ 570:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight AppleScriptlang="applescript">{{-5, -3, -1, 1, 3, 5}, {-6, -4, -2, 0, 2, 4, 6}}</langsyntaxhighlight>
 
=={{header|Arendelle}}==
Line 574 ⟶ 584:
"| @input | is odd!"
}</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI or android 32 bits */
/* program oddEven.s */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
Line 597 ⟶ 608:
sMessResultEven: .asciz " @ is even (pair) \n"
szCarriageReturn: .asciz "\n"
 
/*********************************/
/* UnInitialized data */
Line 607 ⟶ 618:
/*********************************/
.text
.global main
main: @ entry of program
 
mov r0,#5
Line 616 ⟶ 627:
mov r0,#2021
bl testOddEven
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
 
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsMessResultOdd: .int sMessResultOdd
Line 630 ⟶ 641:
// r0 contains à number
testOddEven:
push {r2-r8,lr} @ save registers
tst r0,#1 @ test bit 0 to one
beq 1f @ if result are all zéro, go to even
Line 654 ⟶ 665:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
 
=={{header|ArnoldC}}==
<langsyntaxhighlight lang="arnoldc">LISTEN TO ME VERY CAREFULLY isOdd
I NEED YOUR CLOTHES YOUR BOOTS AND YOUR MOTORCYCLE n
GIVE THESE PEOPLE AIR
Line 688 ⟶ 699:
DO IT NOW showParity 6
DO IT NOW showParity -11
YOU HAVE BEEN TERMINATED</langsyntaxhighlight>
{{out}}
<pre>
Line 702 ⟶ 713:
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">loop (neg 5)..5 [x][
if? even? x -> print [pad to :string x 4 ": even"]
else -> print [pad to :string x 4 ": odd"]
]</langsyntaxhighlight>
{{out}}
 
<pre> -5 : odd
-4 : even
Line 723 ⟶ 732:
 
=={{header|Asymptote}}==
<langsyntaxhighlight Asymptotelang="asymptote">for (int i = 1; i <= 10; ++i) {
if (i % 2 == 0) {
write(string(i), " is even");
Line 729 ⟶ 738:
write(string(i), " is odd");
}
}</langsyntaxhighlight>
 
 
=={{header|AutoHotkey}}==
Bitwise ops are probably most efficient:
<langsyntaxhighlight AHKlang="ahk">if ( int & 1 ){
; do odd stuff
}else{
; do even stuff
}</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight AWKlang="awk">function isodd(x) {
return (x % 2) !=0; 0
}
 
function iseven(x) {
return (x % 2) ==0; 0
}</langsyntaxhighlight>
 
=={{header|BaConBASIC}}==
==={{header|Applesoft BASIC}}===
<lang freebasic>' Even or odd
<syntaxhighlight lang="basic">10 INPUT "ENTER A NUMBER: ";N
20 IF N/2 <> INT(N/2) THEN PRINT "THE NUMBER IS ODD":GOTO 40
30 PRINT "THE NUMBER IS EVEN"
40 END</syntaxhighlight>
{{works with|Commodore BASIC|2.0}}
 
==={{header|BaCon}}===
<syntaxhighlight lang="freebasic">' Even or odd
OPTION MEMTYPE int
SPLIT ARGUMENT$ BY " " TO arg$ SIZE dim
n = IIF$(dim < 2, 0, VAL(arg$[1]))
PRINT n, " is ", IIF$(EVEN(n), "even", "odd")</langsyntaxhighlight>
 
{{out}}
<pre>prompt$ ./even-or-odd 42
Line 762 ⟶ 778:
41 is odd</pre>
 
==={{header|BASICBASIC256}}===
==={{headerworks with|ApplesoftTrue BASIC}}===
<syntaxhighlight lang="basic256">for i = 1 to 10
if (i mod 2) then print i;" is odd" else print i;" is even"
next i
end</syntaxhighlight>
 
==={{header|BBC BASIC}}===
<lang basic>10 INPUT "ENTER A NUMBER: ";N
{{works with|BBC BASIC for Windows}}
20 IF N/2 <> INT(N/2) THEN PRINT "THE NUMBER IS ODD":GOTO 40
Solutions using AND or MOD are restricted to 32-bit integers, so an alternative solution is given which works with a larger range of values.
30 PRINT "THE NUMBER IS EVEN"
<syntaxhighlight lang="bbcbasic"> IF FNisodd%(14) PRINT "14 is odd" ELSE PRINT "14 is even"
40 END</lang>
IF FNisodd%(15) PRINT "15 is odd" ELSE PRINT "15 is even"
{{works with|Commodore BASIC|2.0}}
IF FNisodd#(9876543210#) PRINT "9876543210 is odd" ELSE PRINT "9876543210 is even"
IF FNisodd#(9876543211#) PRINT "9876543211 is odd" ELSE PRINT "9876543211 is even"
END
 
REM Works for -2^31 <= n% < 2^31
==={{header|Commodore BASIC}}===
DEF FNisodd%(n%) = (n% AND 1) <> 0
 
REM Works for -2^53 <= n# <= 2^53
DEF FNisodd#(n#) = n# <> 2 * INT(n# / 2)</syntaxhighlight>
{{out}}
<pre>
14 is even
15 is odd
9876543210 is even
9876543211 is odd
</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
Uses bitwise AND as suggested.
<syntaxhighlight lang="qbasic">10 cls
20 for n = 1 to 10
30 print n;
40 if (n and 1) = 1 then print "is odd" else print "is even"
50 next n
60 end</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
<lang gwbasic>10 rem determine if integer is even or odd
Uses bitwise AND as suggested.
<syntaxhighlight lang="gwbasic">10 rem determine if integer is even or odd
20 print "Enter an integer:";
30 input i%
Line 781 ⟶ 825:
40 eo$="even"
50 if (i% and 1)=1 then eo$="odd"
60 print "The number ";i%;"is ";eo$;"."</langsyntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
Dim n As Integer
 
Do
Print "Enter an integer or 0 to finish : ";
Input "", n
If n = 0 Then
Exit Do
ElseIf n Mod 2 = 0 Then
Print "Your number is even"
Print
Else
Print "Your number is odd"
Print
End if
Loop
 
End</syntaxhighlight>
 
==={{header|Gambas}}===
<syntaxhighlight lang="gambas">Public Sub Form_Open()
Dim sAnswer, sMessage As String
 
sAnswer = InputBox("Input an integer", "Odd or even")
 
If IsInteger(sAnswer) Then
If Odd(Val(sAnswer)) Then sMessage = "' is an odd number"
If Even(Val(sAnswer)) Then sMessage = "' is an even number"
Else
sMessage = "' does not compute!!"
Endif
 
Print "'" & sAnswer & sMessage
 
End</syntaxhighlight>
 
Output:
<pre>
'25' is an odd number
'100' is an even number
'Fred' does not compute!!
</pre>
 
==={{header|GW-BASIC}}===
<langsyntaxhighlight lang="gwbasic">
10 INPUT "Enter a number: ", N
20 IF N MOD 2 = 1 THEN PRINT "It is odd." ELSE PRINT "It is even."</langsyntaxhighlight>
 
==={{header|IS-BASIC}}===
<langsyntaxhighlight ISlang="is-BASICbasic">100 DEF ODD(X)=MOD(X,2)
110 INPUT PROMPT "Enter a number: ":X
120 IF ODD(X) THEN
Line 795 ⟶ 884:
140 ELSE
150 PRINT X;"is even."
160 END IF</langsyntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">n=12
 
if n mod 2 = 0 then print "even" else print "odd"</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{works with|IS-BASIC}}
<lang gwbasic>
<syntaxhighlight lang="gwbasic">10 REM Even or odd
20 PRINT "Enter an integer number";
30 INPUT N
40 IF N/2 <> INT(N/2) THEN 70
Line 806 ⟶ 901:
60 GOTO 80
70 PRINT "The number is odd."
80 END</syntaxhighlight>
 
</lang>
==={{header|MSX Basic}}===
Uses bitwise AND as suggested.
<syntaxhighlight lang="qbasic">10 CLS
20 FOR N = -5 TO 5
30 PRINT N;
40 IF (N AND 1) = 1 THEN PRINT "is odd" ELSE PRINT "is even"
50 NEXT N
60 END</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">;use last bit method
isOdd = i & 1 ;isOdd is non-zero if i is odd
isEven = i & 1 ! 1 ;isEven is non-zero if i is even
 
;use modular method
isOdd = i % 2 ;isOdd is non-zero if i is odd
isEven = i % 2 ! 1 ;isEven is non-zero if i is even</syntaxhighlight>
 
==={{header|QB64}}===
NB: Line numbers are not required in this language. Further, because of the Int variable type used for input, floating point values will not be accepted by the program. 0 is a problem, though, as it returns "Even" in the code below, even though it is not mathematically an even value. For code brevity, the 0 problem is not addressed. Finally, No Even or Odd predicates exist in this language.
<langsyntaxhighlight QB64lang="qb64">'This is a comment line. It also could have been preceded with "Rem"
 
Dim i% 'This line is not necessary, but % strict casts
'as an Int (2 bytes). "As Int" could have been used instead.
Input "#? ", i% 'Prints "#? " as a prompt and waits
'for user input terminated by pressing [ENTER].
 
'Binary integers example
If i% And 1 Then 'Test whether the input value AND 1 is 0 (false) or 1 (true).
'There is no global or constant "True" or "False".
Print "Odd" 'Prints "Odd" if the above tested "true".
Else 'This could have been also been "ElseIf Not (i% And 1)"
Print "Even" 'Prints "Even in all other cases (Else)
'or if the logical inverse of the input value AND 1 tested
'"true" (ElseIf).
Line 833 ⟶ 945:
Else
Print "Still Even"
End If</langsyntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|Run BASIC}}
<syntaxhighlight lang="qbasic">FOR i = 1 TO 10
IF i AND 1 THEN PRINT i; " is odd" ELSE PRINT i; " is even"
NEXT i</syntaxhighlight>
 
==={{header|Quite BASIC}}===
<syntaxhighlight lang="qbasic">10 CLS
20 FOR n = -5 TO 5
30 PRINT n;
40 IF n % 2 <> 0 THEN PRINT " is odd" ELSE PRINT " is even"
50 NEXT n
60 END</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|QBasic}}
<syntaxhighlight lang="runbasic">for i = 1 to 10
if i and 1 then print i;" is odd" else print i;" is even"
next i</syntaxhighlight>
<pre>1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even</pre>
 
==={{header|S-BASIC}}===
S-BASIC lacks a MOD operator but supports bitwise operations on integer variables, so that is the approach taken.
<syntaxhighlight lang="basic">
rem - return true (-1) if even, otherwise false (0)
function even(i = integer) = integer
var one = integer rem - both operands must be variables
one = 1
end = ((i and one) = 0)
 
rem - exercise the function
var i = integer
for i = 1 to 10 step 3
print i; " is ";
if even(i) then
print "even"
else
print "odd"
next
 
end</syntaxhighlight>
{{out}}
<pre> 1 is odd
4 is even
7 is odd
10 is even</pre>
 
==={{header|TI-83 BASIC}}===
TI-83 BASIC does not have a modulus operator.
<syntaxhighlight lang="ti83b">If fPart(.5Ans
Then
Disp "ODD
Else
Disp "EVEN
End</syntaxhighlight>
 
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
<lang>10 PRINT "Enter a number:"
<syntaxhighlight lang="basic">10 PRINT "Enter a number:"
20 INPUT N
30 IF 2*(N/2) = N THEN GOTO 60
40 PRINT "It's odd."
50 END
60 PRINT "It's even."</lang>
70 END</syntaxhighlight>
 
==={{header|True BASIC}}===
{{works with|BASIC256}}
<syntaxhighlight lang="qbasic">FOR i = 1 to 10
IF MOD(i, 2) = 0 THEN PRINT i; " is odd" ELSE PRINT i; " is even"
NEXT i
END</syntaxhighlight>
 
==={{header|VBA}}===
<pre>4 ways = 4 Functions :
IsEven ==> Use the even and odd predicates
IsEven2 ==> Check the least significant digit. With binary integers, i bitwise-and 1 equals 0 iff i is even
IsEven3 ==> Divide i by 2. The remainder equals 0 if i is even.
IsEven4 ==> Use modular congruences</pre>
 
<syntaxhighlight lang="vb">Option Explicit
 
Sub Main_Even_Odd()
Dim i As Long
 
For i = -50 To 48 Step 7
Debug.Print i & " : IsEven ==> " & IIf(IsEven(i), "is even", "is odd") _
& " " & Chr(124) & " IsEven2 ==> " & IIf(IsEven2(i), "is even", "is odd") _
& " " & Chr(124) & " IsEven3 ==> " & IIf(IsEven3(i), "is even", "is odd") _
& " " & Chr(124) & " IsEven4 ==> " & IIf(IsEven4(i), "is even", "is odd")
Next
End Sub
 
Function IsEven(Number As Long) As Boolean
'Use the even and odd predicates
IsEven = (WorksheetFunction.Even(Number) = Number)
End Function
 
Function IsEven2(Number As Long) As Boolean
'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.
Dim lngTemp As Long
lngTemp = CLng(Right(CStr(Number), 1))
If (lngTemp And 1) = 0 Then IsEven2 = True
End Function
 
Function IsEven3(Number As Long) As Boolean
'Divide i by 2.
'The remainder equals 0 if i is even.
Dim sngTemp As Single
sngTemp = Number / 2
IsEven3 = ((Int(sngTemp) - sngTemp) = 0)
End Function
 
Function IsEven4(Number As Long) As Boolean
'Use modular congruences
IsEven4 = (Number Mod 2 = 0)
End Function</syntaxhighlight>
{{out}}
<pre>-50 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
-43 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
-36 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
-29 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
-22 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
-15 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
-8 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
-1 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
6 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
13 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
20 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
27 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
34 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
41 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
48 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even</pre>
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">Function odd_or_even(n)
If n Mod 2 = 0 Then
odd_or_even = "Even"
Else
odd_or_even = "Odd"
End If
End Function
 
WScript.StdOut.Write "Please enter a number: "
n = WScript.StdIn.ReadLine
WScript.StdOut.Write n & " is " & odd_or_even(CInt(n))
WScript.StdOut.WriteLine</syntaxhighlight>
{{Out}}
<pre>C:\>cscript /nologo odd_or_even.vbs
Please enter a number: 6
6 is Even
 
C:\>cscript /nologo odd_or_even.vbs
Please enter a number: 9
9 is Odd
 
C:\>cscript /nologo odd_or_even.vbs
Please enter a number: -1
-1 is Odd</pre>
 
==={{header|Visual Basic .NET}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="vbnet">Module Module1
 
Sub Main()
Dim str As String
Dim num As Integer
While True
Console.Write("Enter an integer or 0 to finish: ")
str = Console.ReadLine()
If Integer.TryParse(str, num) Then
If num = 0 Then
Exit While
End If
If num Mod 2 = 0 Then
Console.WriteLine("Even")
Else
Console.WriteLine("Odd")
End If
Else
Console.WriteLine("Bad input.")
End If
End While
End Sub
 
End Module</syntaxhighlight>
==== BigInteger ====
{{Libheader|System.Numerics}}
<syntaxhighlight lang="vbnet">Imports System.Numerics
 
Module Module1
Function IsOdd(bi As BigInteger) As Boolean
Return Not bi.IsEven
End Function
 
Function IsEven(bi As BigInteger) As Boolean
Return bi.IsEven
End Function
 
Sub Main()
' uncomment one of the following Dim statements
' Dim x As Byte = 3
' Dim x As Short = 3
' Dim x As Integer = 3
' Dim x As Long = 3
' Dim x As SByte = 3
' Dim x As UShort = 3
' Dim x As UInteger = 3
' Dim x As ULong = 3
' Dim x as BigInteger = 3
' the following three types give a warning, but will work
' Dim x As Single = 3
' Dim x As Double = 3
' Dim x As Decimal = 3
 
Console.WriteLine("{0} {1}", IsOdd(x), IsEven(x))
End Sub
End Module</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="xbasic">PROGRAM "Even/Odd"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
FOR i = 1 TO 10
IF (i MOD 2) THEN PRINT i;" is odd" ELSE PRINT i;" is even"
NEXT i
END FUNCTION
 
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|Phix}}
<syntaxhighlight lang="yabasic">for i = -5 to 5
print i, and(i,1), mod(i,2)
next
</syntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
<syntaxhighlight lang="zxbasic">10 FOR n=-3 TO 4: GO SUB 30: NEXT n
20 STOP
30 LET odd=FN m(n,2)
40 PRINT n;" is ";("Even" AND odd=0)+("Odd" AND odd=1)
50 RETURN
60 DEF FN m(a,b)=a-INT (a/b)*b</syntaxhighlight>
 
=={{header|Batch File}}==
<syntaxhighlight lang="dos">@echo off
<lang dos>
set /p i=Insert number:
@echo off
set /p i=Insert number:
 
::bitwise and
Line 858 ⟶ 1,221:
 
set test
pause>nul</syntaxhighlight>
</lang>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
Solutions using AND or MOD are restricted to 32-bit integers, so an alternative solution is given which works with a larger range of values.
<lang bbcbasic> IF FNisodd%(14) PRINT "14 is odd" ELSE PRINT "14 is even"
IF FNisodd%(15) PRINT "15 is odd" ELSE PRINT "15 is even"
IF FNisodd#(9876543210#) PRINT "9876543210 is odd" ELSE PRINT "9876543210 is even"
IF FNisodd#(9876543211#) PRINT "9876543211 is odd" ELSE PRINT "9876543211 is even"
END
REM Works for -2^31 <= n% < 2^31
DEF FNisodd%(n%) = (n% AND 1) <> 0
REM Works for -2^53 <= n# <= 2^53
DEF FNisodd#(n#) = n# <> 2 * INT(n# / 2)</lang>
{{out}}
<pre>
14 is even
15 is odd
9876543210 is even
9876543211 is odd
</pre>
 
=={{header|bc}}==
There are no bitwise operations, so this solution compares a remainder with zero. Calculation of ''i % 2'' only works when ''scale = 0''.
<langsyntaxhighlight lang="bc">i = -3
 
/* Assumes that i is an integer. */
Line 892 ⟶ 1,232:
"
if (i % 2) "i is odd
"</langsyntaxhighlight>
 
=={{header|Beads}}==
<langsyntaxhighlight lang="beads">beads 1 program 'Even or odd'
 
calc main_init
loop across:[-10, -5, 10, 5] val:v
log "{v}\todd:{is_odd(v)}\teven:{is_even(v)}"</langsyntaxhighlight>
 
{{out}}
Line 910 ⟶ 1,250:
 
=={{header|Befunge}}==
<langsyntaxhighlight lang="befunge">&2%52**"E"+,@</langsyntaxhighlight>
 
Outputs E if even, O if odd.
 
=={{header|Binary Lambda Calculus}}==
In lambda calculus, the oddness of a given church numeral n can be computed as n applications of <code>not</code> to <code>false</code>: <code>\n. n (\b\x\y. b y x) (\x\y.y)</code>, which in BLC is
 
<pre>00 01 01 10 0000000101111010110 000010</pre>
 
To compute the evenness, one need only replace <code>false</code> by <code>true</code>, i.e. replace the final 0 bit by 10.
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">
odd ← 2⊸|
 
!0 ≡ odd 12
!1 ≡ odd 31</langsyntaxhighlight>
 
=={{header|Bracmat}}==
Not the simplest solution, but the cheapest if the number that must be tested has thousands of digits.
<langsyntaxhighlight lang="bracmat">( ( even
=
. @( !arg
Line 951 ⟶ 1,298:
& eventest$857234098750432987502398457089435
& oddtest$857234098750432987502398457089435
)</langsyntaxhighlight>
{{out}}
<pre>5556 is even
Line 959 ⟶ 1,306:
 
=={{header|Brainf***}}==
Assumes that input characters are an ASCII representation of a valid integer.
Output is input <tt>mod</tt> 2.
<langsyntaxhighlight lang="bf">,[>,----------] Read until newline
++< Get a 2 and move into position
[->-[>+>>]> Do
Line 968 ⟶ 1,315:
>[-]<++++++++ Clear and get an 8
[>++++++<-] to get a 48
>[>+<-]>. to get n % 2 to ASCII and print</langsyntaxhighlight>
 
If one need only determine rather than act on the parity of the input,
the following is sufficient; it terminates either quickly or never.
<langsyntaxhighlight lang="bf">,[>,----------]<[--]</langsyntaxhighlight>
 
=={{header|Burlesque}}==
<syntaxhighlight lang ="burlesque">2.%</langsyntaxhighlight>
 
=={{header|C}}==
Test by bitwise and'ing 1, works for any builtin integer type as long as it's 2's complement (it's always so nowadays):
<langsyntaxhighlight lang="c">if (x & 1) {
/* x is odd */
} else {
/* or not */
}</langsyntaxhighlight>
If using long integer type from GMP (<code>mpz_t</code>), there are provided macros:
<langsyntaxhighlight lang="c">mpz_t x;
...
if (mpz_even_p(x)) { /* x is even */ }
if (mpz_odd_p(x)) { /* x is odd */ }</langsyntaxhighlight>
The macros evaluate <code>x</code> more than once, so it should not be something with side effects.
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">namespace RosettaCode
{
using System;
Line 1,057 ⟶ 1,404:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
Test using the modulo operator, or use the C example from above.
<syntaxhighlight lang ="cpp">bool isOdd(int x)
{
return x % 2;
Line 1,069 ⟶ 1,416:
{
return !(x % 2);
}</langsyntaxhighlight>
 
A slightly more type-generic version, for C++11 and later. This should theoretically work for any type convertible to <code>int</code>:
 
<langsyntaxhighlight lang="cpp">
template < typename T >
constexpr inline bool isEven( const T& v )
{
Line 1,080 ⟶ 1,427:
}
 
template <>
constexpr inline bool isEven< int >( const int& v )
{
Line 1,086 ⟶ 1,433:
}
 
template < typename T >
constexpr inline bool isOdd( const T& v )
{
return !isEven(v);
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
Standard predicates:
<langsyntaxhighlight lang="clojure">(if (even? some-var) (do-even-stuff))
(if (odd? some-var) (do-odd-stuff))</langsyntaxhighlight>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IF FUNCTION REM(Num, 2) = 0
DISPLAY Num " is even."
ELSE
DISPLAY Num " is odd."
END-IF</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
<langsyntaxhighlight lang="coffeescript">isEven = (x) -> !(x%2)</langsyntaxhighlight>
 
=={{header|ColdFusion}}==
<langsyntaxhighlight lang="cfm">
function f(numeric n) {
return n mod 2?"odd":"even"
}
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
Standard predicates:
<langsyntaxhighlight lang="lisp">(if (evenp some-var) (do-even-stuff))
(if (oddp some-other-var) (do-odd-stuff))</langsyntaxhighlight>
===Alternate solution===
I use [https://franz.com/downloads/clp/survey Allegro CL 10.1]
 
<langsyntaxhighlight lang="lisp">
;; Project : Even or odd
 
Line 1,128 ⟶ 1,475:
(cond ((evenp nr) "even")
((oddp nr) "odd")))
(dotimes (n 10)
(if (< n 1) (terpri))
(if (< n 9) (format t "~a" " "))
(write(+ n 1)) (format t "~a" ": ")
(format t "~a" (evenodd (+ n 1))) (terpri))
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,150 ⟶ 1,497:
=={{header|Component Pascal}}==
BlackBox Component Builder
<langsyntaxhighlight lang="oberon2">
MODULE EvenOdd;
IMPORT StdLog,Args,Strings;
Line 1,178 ⟶ 1,525:
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)
Line 1,185 ⟶ 1,532:
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)
Line 1,192 ⟶ 1,539:
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)
Line 1,199 ⟶ 1,546:
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;
</syntaxhighlight>
</lang>
Execute: ^Q EvenOdd.Do 10 11 0 57 34 -23 -42~<br/>
{{out}}
Line 1,243 ⟶ 1,590:
 
=={{header|Crystal}}==
<langsyntaxhighlight lang="crystal">#Using bitwise shift
def isEven_bShift(n)
n == ((n >> 1) << 1)
Line 1,273 ⟶ 1,620:
puts isEven_bAnd(21)
puts isOdd_bAnd(21)
</syntaxhighlight>
</lang>
{{out}}
<pre>false
Line 1,284 ⟶ 1,631:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.bigint;
 
foreach (immutable i; -5 .. 6)
writeln(i, " ", i & 1, " ", i % 2, " ", i.BigInt % 2);
}</langsyntaxhighlight>
{{out}}
<pre>-5 1 -1 -1
Line 1,302 ⟶ 1,649:
4 0 0 0
5 1 1 1</pre>
 
=={{header|Dart}}==
<syntaxhighlight lang="dart">void main() {
for (var i = 1; i <= 10; i++) {
if (i % 2 != 0) {
print("$i is odd");
} else {
print("$i is even");
}
}
}</syntaxhighlight>
 
=={{header|dc}}==
This macro expects an integer on the stack, pops it, and pushes 1 if it is odd, or 0 if it is even (independently from the precision currently set).
<syntaxhighlight lang="dc">[K Sk 0 k 2 % Lk k]</syntaxhighlight>
 
=={{header|DCL}}==
<langsyntaxhighlight DCLlang="dcl">$! in DCL, for integers, the least significant bit determines the logical value, where 1 is true and 0 is false
$
$ i = -5
Line 1,311 ⟶ 1,673:
$ if .not. i then $ write sys$output i, " is even"
$ i = i + 1
$ if i .le. 6 then $ goto loop1</langsyntaxhighlight>
{{out}}
<pre>$ @even_odd
Line 1,328 ⟶ 1,690:
 
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">
<lang Delphi>
program EvenOdd;
 
Line 1,358 ⟶ 1,720:
Readln;
end.
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,378 ⟶ 1,740:
10 is even
</pre>
=={{header|DWScript}}==
Predicate:
<lang delphi>var isOdd := Odd(i);</lang>
Bitwise and:
<lang delphi>var isOdd := (i and 1)<>0;</lang>
Modulo:
<lang delphi>var isOdd := (i mod 2)=1;</lang>
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu">even n:
= 0 % n 2
 
Line 1,397 ⟶ 1,752:
!. odd 7
!. even 7
</syntaxhighlight>
</lang>
{{out}}
<pre>false
Line 1,403 ⟶ 1,758:
true
false</pre>
 
=={{header|Diego}}==
<syntaxhighlight lang="diego">use_namespace(rosettacode)_me();
 
funct(isEven)_arg(i)_ret()_calc(i%2)_equals(0);
 
reset_namespace[];</syntaxhighlight>
 
=={{header|DWScript}}==
Predicate:
<syntaxhighlight lang="delphi">var isOdd := Odd(i);</syntaxhighlight>
Bitwise and:
<syntaxhighlight lang="delphi">var isOdd := (i and 1)<>0;</syntaxhighlight>
Modulo:
<syntaxhighlight lang="delphi">var isOdd := (i mod 2)=1;</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
a = 13
if a mod 2 = 0
print a & " is even"
else
print a & " is odd"
.
</syntaxhighlight>
 
=={{header|EDSAC order code}}==
This implementation uses the <code>C</code> (logical AND multiplier register with memory) order. It will cause the machine to print an <tt>E</tt> if the number stored at address <i>θ</i>+15 is even, or an <tt>O</tt> if it is odd. As an example, we shall test the number 37 (<code>P18D</code> in EDSAC encoding).
<langsyntaxhighlight lang="edsac">[ Even or odd
===========
 
Line 1,438 ⟶ 1,818:
[ 15 ] P18D [ number to test: 37 ]
 
EZPF [ branch to load point ]</langsyntaxhighlight>
{{out}}
<pre>O</pre>
 
=={{header|Eiffel}}==
<langsyntaxhighlight Eiffellang="eiffel">--bit testing
if i.bit_and (1) = 0 then
-- i is even
Line 1,456 ⟶ 1,836:
if i \\ 2 = 0 then
-- i is even
end</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">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
Line 1,472 ⟶ 1,852:
end
 
Enum.each(-2..3, fn n -> IO.puts RC.even_or_odd(n) end)</langsyntaxhighlight>
 
{{out}}
Line 1,484 ⟶ 1,864:
</pre>
Other ways to test even-ness:
<langsyntaxhighlight lang="elixir">rem(n,2) == 0</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight Lisplang="lisp">(require 'cl-lib)
 
(defun even-or-odd-p (n)
Line 1,496 ⟶ 1,876:
 
(message "%d is %s" 3 (even-or-oddp 3))
(message "%d is %s" 2 (even-or-oddp 2))</langsyntaxhighlight>
 
{{out}}
Line 1,502 ⟶ 1,882:
3 is odd
2 is even
 
 
 
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
List evenCheckers = fun[
logic by int i do return i % 2 == 0 end,
logic by int i do return i & 1 == 0 end]
List oddCheckers = fun[
logic by int i do return i % 2 != 0 end,
logic by int i do return i & 1 == 1 end]
writeLine("integer".padStart(10, " ") + "|is_even" + "|is_odd |")
writeLine("----------+-------+-------+")
for each int i in range(-5, 6).append(3141592653)
write((text!i).padStart(10, " ") + "| ")
for each fun isEven in evenCheckers
write(isEven(i) + " ")
end
write("| ")
for each fun isOdd in oddCheckers
write(isOdd(i) + " ")
end
writeLine("|")
end
writeLine("----------+-------+-------+")
</syntaxhighlight>
{{out}}
<pre>
integer|is_even|is_odd |
----------+-------+-------+
-5| ⊥ ⊥ | ⊤ ⊤ |
-4| ⊤ ⊤ | ⊥ ⊥ |
-3| ⊥ ⊥ | ⊤ ⊤ |
-2| ⊤ ⊤ | ⊥ ⊥ |
-1| ⊥ ⊥ | ⊤ ⊤ |
0| ⊤ ⊤ | ⊥ ⊥ |
1| ⊥ ⊥ | ⊤ ⊤ |
2| ⊤ ⊤ | ⊥ ⊥ |
3| ⊥ ⊥ | ⊤ ⊤ |
4| ⊤ ⊤ | ⊥ ⊥ |
5| ⊥ ⊥ | ⊤ ⊤ |
3141592653| ⊥ ⊥ | ⊤ ⊤ |
----------+-------+-------+
</pre>
 
=={{header|Erlang}}==
===Using Division by 2 Method===
<langsyntaxhighlight lang="erlang">%% Implemented by Arjun Sunel
-module(even_odd).
-export([main/0]).
Line 1,517 ⟶ 1,942:
true ->
io:format("even\n")
end.
</syntaxhighlight>
</lang>
===Using the least-significant bit method===
<langsyntaxhighlight lang="erlang"> %% Implemented by Arjun Sunel
-module(even_odd2).
-export([main/0]).
Line 1,532 ⟶ 1,957:
true ->
io:format("even\n")
end.
</syntaxhighlight>
</lang>
 
=={{header|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM ODD_EVEN
 
! works for -2^15 <= n% < 2^15
Line 1,555 ⟶ 1,980:
IF ISODD#(9876543211) THEN PRINT("9876543211 is odd") ELSE PRINT("9876543211 is even") END IF
END PROGRAM
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,566 ⟶ 1,991:
=={{header|Euphoria}}==
Using standard function
<syntaxhighlight lang="euphoria">
<lang Euphoria>
include std/math.e
 
Line 1,572 ⟶ 1,997:
? {i, is_even(i)}
end for
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,589 ⟶ 2,014:
=={{header|Excel}}==
Use the MOD function
<syntaxhighlight lang="excel">
<lang Excel>
=MOD(33;2)
=MOD(18;2)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,601 ⟶ 2,026:
 
Use the ISEVEN function, returns TRUE or FALSE
<syntaxhighlight lang="excel">
<lang Excel>
=ISEVEN(33)
=ISEVEN(18)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,613 ⟶ 2,038:
 
Use the ISODD function, returns TRUE or FALSE
<syntaxhighlight lang="excel">
<lang Excel>
=ISODD(33)
=ISODD(18)
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,627 ⟶ 2,052:
 
Bitwise and:
<langsyntaxhighlight lang="fsharp">let isEven x =
x &&& 1 = 0</langsyntaxhighlight>
 
Modulo:
<langsyntaxhighlight lang="fsharp">let isEven x =
x % 2 = 0</langsyntaxhighlight>
 
=={{header|Factor}}==
Line 1,648 ⟶ 2,073:
=={{header|Fish}}==
This example assumes that the input command ''i'' returns an integer when one was inputted and that the user inputs a valid positive integer terminated by a newline.
<langsyntaxhighlight Fishlang="fish"><v"Please enter a number:"a
>l0)?!vo v < v o<
^ >i:a=?v>i:a=?v$a*+^>"The number is even."ar>l0=?!^>
> >2%0=?^"The number is odd."ar ^</langsyntaxhighlight>
The actual computation is the 2%0= part. The rest is either user interface or parsing input.
 
=={{header|Forth}}==
<syntaxhighlight lang="forth">
<lang forth>: odd? ( n -- ? ) 1 and ;</lang>
: odd? ( n -- ? ) 1 and ;
: even? ( n -- ? ) odd? 0= ;
 
\ Every value not equal to zero is considered true. Only zero is considered false.
</syntaxhighlight>
 
=={{header|Fortran}}==
 
Please find the compilation and example run in the comments at the beginning of the FORTRAN 2008 source. Separating the bit 0 parity module from the main program enables reuse of the even and odd functions. Even and odd, with scalar and vector interfaces demonstrate the generic function capability of FORTRAN 90. Threading, stdin, and all-intrinsics are vestigial and have no influence here other than to confuse you.
<syntaxhighlight lang="fortran">
<lang FORTRAN>
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Tue May 21 20:22:56
Line 1,739 ⟶ 2,169:
write(6, '((13i3),a8/(13l3),a8/(13l3),a8)') j, 'n', odd(j), 'odd', even(j), 'even'
end program oe
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<lang freebasic>' FB 1.05.0 Win64
 
Dim n As Integer
 
Do
Print "Enter an integer or 0 to finish : ";
Input "", n
If n = 0 Then
Exit Do
ElseIf n Mod 2 = 0 Then
Print "Your number is even"
Print
Else
Print "Your number is odd"
Print
End if
Loop
 
End</lang>
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">isEven[x is isInteger] := getBit[x,0] == 0
isOdd[x is isInteger] := getBit[x,0] == 1</langsyntaxhighlight>
 
=={{header|Futhark}}==
{{incorrect|Futhark|Futhark's syntax has changed, so this example will not compile}}
 
<syntaxhighlight lang="futhark">
<lang Futhark>
fun main(x: int): bool = (x & 1) == 0
</syntaxhighlight>
</lang>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn OddOrEven( i as NSInteger ) as CFStringRef
CFStringRef result
if ( i mod 2 ) == 0 then result = @"Even" else result = @"Odd"
end fn = result
 
NSUInteger i
 
for i = 1 to 10
printf @"%d is %@", i, fn OddOrEven( i )
next
 
HandleEvents
</syntaxhighlight>
 
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Even_or_odd}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solutions'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
'''Case 1.''' Intrinsic expressions:
In '''[https://formulae.org/?example=Even_or_odd this]''' page you can see the program(s) related to this task and their results.
 
[[File:Fōrmulæ - Even or odd 01.png]]
=={{header|Gambas}}==
<lang gambas>Public Sub Form_Open()
Dim sAnswer, sMessage As String
 
[[File:Fōrmulæ - Even or odd 02.png]]
sAnswer = InputBox("Input an integer", "Odd or even")
 
'''Case 2.''' Using the Divides and DoesNotDivide expressions:
If IsInteger(sAnswer) Then
If Odd(Val(sAnswer)) Then sMessage = "' is an odd number"
If Even(Val(sAnswer)) Then sMessage = "' is an even number"
Else
sMessage = "' does not compute!!"
Endif
 
[[File:Fōrmulæ - Even or odd 03.png]]
Print "'" & sAnswer & sMessage
 
[[File:Fōrmulæ - Even or odd 04.png]]
End</lang>
 
'''Case 3.''' Using modular congruences
Output:
 
<pre>
[[File:Fōrmulæ - Even or odd 05.png]]
'25' is an odd number
 
'100' is an even number
[[File:Fōrmulæ - Even or odd 06.png]]
'Fred' does not compute!!
 
</pre>
'''Case 4.''' Using bitwise operations
 
[[File:Fōrmulæ - Even or odd 07.png]]
 
[[File:Fōrmulæ - Even or odd 08.png]]
 
'''Case 5.''' Using IsRational
 
[[File:Fōrmulæ - Even or odd 09.png]]
 
[[File:Fōrmulæ - Even or odd 10.png]]
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">IsEvenInt(n);
IsOddInt(n);</langsyntaxhighlight>
 
=={{header|Genie}}==
Using bitwise AND of the zero-bit.
<langsyntaxhighlight lang="genie">[indent = 4]
/*
Even or odd, in Genie
Line 1,828 ⟶ 2,259:
show_parity(2)
show_parity(-2)
show_parity(-1)</langsyntaxhighlight>
 
{{out}}
Line 1,840 ⟶ 2,271:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,887 ⟶ 2,318:
fmt.Println("odd")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,904 ⟶ 2,335:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def isOdd = { int i -> (i & 1) as boolean }
def isEven = {int i -> ! isOdd(i) }</langsyntaxhighlight>
Test:
<langsyntaxhighlight lang="groovy">1.step(20, 2) { assert isOdd(it) }
 
50.step(-50, -2) { assert isEven(it) }</langsyntaxhighlight>
 
=={{header|Haskell}}==
<code>even</code> and <code>odd</code> functions are already included in the standard Prelude.
<langsyntaxhighlight lang="haskell">Prelude> even 5
False
Prelude> even 42
Line 1,920 ⟶ 2,351:
True
Prelude> odd 42
False</langsyntaxhighlight>
 
Where '''even''' is derived from '''rem''', and '''odd''' is derived from even:
<langsyntaxhighlight lang="haskell">import Prelude hiding (even, odd)
 
even, odd
Line 1,933 ⟶ 2,364:
 
main :: IO ()
main = print (even <$> [0 .. 9])</langsyntaxhighlight>
{{Out}}
<pre>[True,False,True,False,True,False,True,False,True,False]</pre>
 
=={{header|Hoon}}==
<langsyntaxhighlight Hoonlang="hoon">|= n=@ud
?: =((mod n 2) 0)
"even"
"odd"</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
One way is to check the remainder:
<langsyntaxhighlight lang="unicon">procedure isEven(n)
return n%2 = 0
end</langsyntaxhighlight>
 
=={{header|Insitux}}==
Exactly the same as [[Even_or_odd#Clojure|Clojure]], these are built-in predicates.
<syntaxhighlight lang="insitux">(if (even? some-var) (do-even-stuff))
(if (odd? some-var) (do-odd-stuff))</syntaxhighlight>
 
=={{header|J}}==
Modulo:
<langsyntaxhighlight lang="j"> 2 | 2 3 5 7
0 1 1 1
2|2 3 5 7 + (2^89x)-1
1 0 0 0</langsyntaxhighlight>
Remainder:
<langsyntaxhighlight lang="j"> (= <.&.-:) 2 3 5 7
1 0 0 0
(= <.&.-:) 2 3 5 7+(2^89x)-1
0 1 1 1</langsyntaxhighlight>
Last bit in bit representation:
<langsyntaxhighlight lang="j"> {:"1@#: 2 3 5 7
0 1 1 1
{:"1@#: 2 3 5 7+(2^89x)-1
1 0 0 0</langsyntaxhighlight>
Bitwise and:
<langsyntaxhighlight lang="j"> 1 (17 b.) 2 3 5 7
0 1 1 1</langsyntaxhighlight>
Note: as a general rule, the simplest expressions in J should be preferred over more complex approaches.
 
=={{header|Jakt}}==
<syntaxhighlight lang="jakt">
fn is_even<T>(anon n: T) -> bool => 0 == (n & 1)
 
fn is_odd<T>(anon n: T) -> bool => 0 != (n & 1)
 
fn main() {
for i in 0..11 {
println("{} {} {}", i, is_even(i), is_odd(i))
}
}
</syntaxhighlight>
 
=={{header|Java}}==
Bitwise and:
<langsyntaxhighlight lang="java">public static boolean isEven(int i){
return (i & 1) == 0;
}</langsyntaxhighlight>
Modulo:
<langsyntaxhighlight lang="java">public static boolean isEven(int i){
return (i % 2) == 0;
}</langsyntaxhighlight>
Arbitrary precision bitwise:
<langsyntaxhighlight lang="java">public static boolean isEven(BigInteger i){
return i.and(BigInteger.ONE).equals(BigInteger.ZERO);
}</langsyntaxhighlight>
Arbitrary precision bit test (even works for negative numbers because of the way <code>BigInteger</code> represents the bits of numbers):
<langsyntaxhighlight lang="java">public static boolean isEven(BigInteger i){
return !i.testBit(0);
}</langsyntaxhighlight>
Arbitrary precision modulo:
<langsyntaxhighlight lang="java">public static boolean isEven(BigInteger i){
return i.mod(BigInteger.valueOf(2)).equals(BigInteger.ZERO);
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
===ES5===
Bitwise:
<langsyntaxhighlight lang="javascript">function isEven( i ) {
return (i & 1) === 0;
}
</syntaxhighlight>
</lang>
Modulo:
<langsyntaxhighlight lang="javascript">function isEven( i ) {
return i % 2 === 0;
}
Line 2,007 ⟶ 2,456:
function isEven( i ) {
return !(i % 2);
}</langsyntaxhighlight>
 
===ES6===
Lambda:
<langsyntaxhighlight lang="javascript">// EMCAScript 6
const isEven = x => !(x % 2)</langsyntaxhighlight>
 
or, avoiding type coercion:
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 2,039 ⟶ 2,488:
 
return show([xs.filter(even), xs.filter(odd)]);
})();</langsyntaxhighlight>
 
{{Out}}
<pre>[[-6,-4,-2,0,2,4,6],[-5,-3,-1,1,3,5]]</pre>
 
=={{header|jqJoy}}==
<syntaxhighlight lang="joy">DEFINE
even == 2 rem null;
odd == even not.</syntaxhighlight>
 
=={{header|jq}}==
In practice, to test whether an integer, i, is even or odd in jq, one would typically use: i % 2
 
For example, if it were necessary to have a strictly boolean function that would test if its input is an even integer, one could define:
<langsyntaxhighlight lang="jq">def is_even: type == "number" and floor == 0 and . % 2 == 0;</langsyntaxhighlight>
 
The check that the floor is 0 is necessary as % is defined on floating point numbers.
Line 2,055 ⟶ 2,508:
"is_odd" could be similarly defined:
 
<langsyntaxhighlight lang="jq">def is_odd: type == "number" and floor == 0 and . % 2 == 1;</langsyntaxhighlight>
 
=={{header|Jsish}}==
Using bitwise and of low bit.
 
<langsyntaxhighlight lang="javascript">#!/usr/bin/env jsish
/* Even or Odd, in Jsish */
function isEven(n:number):boolean { return (n & 1) === 0; }
Line 2,080 ⟶ 2,533:
isEven(-13) ==> false
=!EXPECTEND!=
*/</langsyntaxhighlight>
{{out}}
<pre>$ jsish --U isEven.jsi
Line 2,090 ⟶ 2,543:
=={{header|Julia}}==
Built-in functions:
<syntaxhighlight lang ="julia">iseven(i), isodd(i)</langsyntaxhighlight>
 
=={{header|K}}==
The following implementation uses the modulo of
division by 2
<syntaxhighlight lang="k">
<lang K>
oddp: {:[x!2;1;0]} /Returns 1 if arg. is odd
evenp: {~oddp[x]} /Returns 1 if arg. is even
Line 2,104 ⟶ 2,557:
evenp 32
1
</syntaxhighlight>
</lang>
 
 
=={{header|Klingphix}}==
<langsyntaxhighlight Klingphixlang="klingphix">( -5 5 ) [
dup print " " print 2 mod ( ["Odd"] ["Even"] ) if print nl
] for
 
" " input</langsyntaxhighlight>
{{out}}
<pre>-5 Odd
Line 2,127 ⟶ 2,580:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.5-2
 
fun main(args: Array<String>) {
Line 2,139 ⟶ 2,592:
}
}
}</langsyntaxhighlight>
 
=={{header|Lambdatalk}}==
 
<langsyntaxhighlight lang="scheme">
{def is_odd {lambda {:i} {= {% :i 2} 1}}}
-> is_odd
 
Line 2,155 ⟶ 2,608:
{is_even 2}
-> true
</syntaxhighlight>
</lang>
 
=={{header|L++}}==
<langsyntaxhighlight lang="lisp">(defn bool isEven (int x) (return (% x 2)))</langsyntaxhighlight>
 
=={{header|LabVIEW}}==
Line 2,165 ⟶ 2,618:
 
=={{header|Lang5}}==
<langsyntaxhighlight lang="lang5">: even? 2 % not ;
: odd? 2 % ;
1 even? . # 0
1 odd? . # 1</langsyntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">define isoddoreven(i::integer) => {
#i % 2 ? return 'odd'
return 'even'
}
isoddoreven(12)</langsyntaxhighlight>
 
=={{header|LC3 Assembly}}==
Prints <tt>EVEN</tt> if the number stored in <tt>NUM</tt> is even, otherwise <tt>ODD</tt>.
<langsyntaxhighlight lang="lc3asm"> .ORIG 0x3000
 
LD R0,NUM
Line 2,199 ⟶ 2,652:
ODD .STRINGZ "ODD\n"
 
.END</langsyntaxhighlight>
 
=={{header|Liberty BASIC}}==
<lang lb>n=12
 
if n mod 2 = 0 then print "even" else print "odd"</lang>
 
=={{header|Lingo}}==
<syntaxhighlight lang="lingo">on even (n)
 
<lang lingo>on even (n)
return n mod 2 = 0
end
Line 2,214 ⟶ 2,661:
on odd (n)
return n mode 2 <> 0
end</langsyntaxhighlight>
 
=={{header|Little Man Computer}}==
Line 2,220 ⟶ 2,667:
 
LMC has no division instruction. To divide by 2 we could use repeated subtraction:
<syntaxhighlight lang="little man computer">
<lang Little Man Computer>
// Input number; output its residue mod 2
INP // read input into acc
Line 2,234 ⟶ 2,681:
k2 DAT 2 // constant 2
// end
</syntaxhighlight>
</lang>
The above program might need 500 subtractions before it found the result. To speed things up we could use something along the following lines.
<syntaxhighlight lang="little man computer">
<lang Little Man Computer>
// Input number; output its residue mod 2
INP // read input into accumulator
Line 2,258 ⟶ 2,705:
save_acc DAT
// end
</syntaxhighlight>
</lang>
Note: LMC, in its original form, does not support negative numbers. If the accumulator contains a number X, and a number Y > X is subtracted, then the negative flag is set and the value in the accumulator becomes undefined. So we can't assume that adding Y back to the accumulator will restore the value of X. If we want to use X again, we need to save it in RAM before doing the subtraction.
 
 
=={{header|LiveCode}}==
<langsyntaxhighlight LiveCodelang="livecode">function odd n
return (n bitand 1) = 1
end odd
Line 2,269 ⟶ 2,716:
function notEven n
return (n mod 2) = 1
end notEven</langsyntaxhighlight>
 
=={{header|LLVM}}==
<langsyntaxhighlight lang="llvm">; This is not strictly LLVM, as it uses the C library function "printf".
; LLVM does not provide a way to print values, so the alternative would be
; to just load the string into memory, and that would be boring.
Line 2,324 ⟶ 2,771:
}
 
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }</langsyntaxhighlight>
{{out}}
<pre>0 is even
Line 2,332 ⟶ 2,779:
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to even? :num
output equal? 0 modulo :num 2
end</langsyntaxhighlight>
 
=={{header|Logtalk}}==
<langsyntaxhighlight lang="logtalk">
:- object(even_odd).
 
Line 2,355 ⟶ 2,802:
 
:- end_object.
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="text">
| ?- even_odd::test_mod(1).
odd
Line 2,373 ⟶ 2,820:
even
yes
</syntaxhighlight>
</lang>
 
=={{header|LOLCODE}}==
<langsyntaxhighlight LOLCODElang="lolcode">HAI 1.4
I HAS A integer
GIMMEH integer
Line 2,387 ⟶ 2,834:
VISIBLE "The integer is even."
OIC
KTHXBYE</langsyntaxhighlight>
 
=={{header|Lua}}==
 
<langsyntaxhighlight lang="lua">-- test for even number
if n % 2 == 0 then
print "The number is even"
Line 2,399 ⟶ 2,846:
if not (n % 2 == 0) then
print "The number is odd"
end</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Binary.Add take any numeric type, but value mustfrom benewer inversions can trait as unsigned with range of 0 to 0xFFFFFFFF (so if the number is out of this range, a cut made).
 
Print binary.and(0xFFFFFFF+10, 0XF)=9 // 0xFFFFFFFF is type Currency and return 4294967295, number 10 is type double so the final number for addition is type double.
 
Print binary.and(0xFFFFFFF&+10, 0XF)=9 // 0xFFFFFFFF& is type long and return -1, number 10 is type double so the final number for addition is type double.
 
Print binary.and(0x7FFFFFFF&*16&+10&, 0xF)=15 // 0x7FFFFFFF&*16& cant fit in long so it is type of double 34359738352 (this performed automatic). But if we give this Long A=0x7FFFFFFF&*16& we get an overflow error, because A is a Long, and 34359738352 can't fit.
 
So Mod if a perfect choice, using it with Decimals (character @ indicate a Decimal type or literal).
Variable a take the type of input. There is no reason here to write it as def Odd(a as decimal)= binary.and(Abs(a), 1)=1
 
Def used to define variables (an error occur if same variable exist), or to define one line local functions. If a function exist then replace code. This is the same for modules/functions, a newer definition alter an old definition with same name, in current module if they are local, or global if they defined as global, like this Function Global F(x) { code block here}.:
 
Function Global F(x) { code block here}.
 
A function F(x) {} is same as
 
<pre >
Function F {
Line 2,415 ⟶ 2,872:
}
</pre >
 
The same hold for Def Odd(a)=binary.and(Abs(a), 1)=1
Interpreter execute this:
Line 2,427 ⟶ 2,884:
So here is the task. Show an overflow from a decimal, then change function.
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckOdd {
Def Odd(a)= binary.and(Abs(a), 1)=1
Print Odd(-5), Odd(6), Odd(11)
Print Odd(21212121212122122122121@)
Try {
Print Odd(21212121212122122122121@)
}
Print Error$ ' overflow
def Odd(a)= Int(Abs(a)) mod 2 =1
Print Odd(21212121212122122122121@)
Line 2,441 ⟶ 2,894:
}
CheckOdd
</syntaxhighlight>
</lang>
 
=={{header|M4}}==
<langsyntaxhighlight M4lang="m4">define(`even', `ifelse(eval(`$1'%2),0,True,False)')
define(`odd', `ifelse(eval(`$1'%2),0,False,True)')
 
Line 2,451 ⟶ 2,904:
 
odd(5)
odd(0)</langsyntaxhighlight>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">EvenOrOdd := proc( x::integer )
if x mod 2 = 0 then
print("Even"):
Line 2,461 ⟶ 2,914:
end if:
end proc:
EvenOrOdd(9);</langsyntaxhighlight>
<pre>"Odd"</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">EvenQ[8]</langsyntaxhighlight>
 
=={{header|MATLAB}} / {{header|Octave}}==
Bitwise And:
<langsyntaxhighlight Matlablang="matlab"> isOdd = logical(bitand(N,1));
isEven = ~logical(bitand(N,1)); </langsyntaxhighlight>
Remainder of division by two :
<langsyntaxhighlight Matlablang="matlab"> isOdd = logical(rem(N,2));
isEven = ~logical(rem(N,2)); </langsyntaxhighlight>
Modulo: 2
<langsyntaxhighlight Matlablang="matlab"> isOdd = logical(mod(N,2));
isEven = ~logical(mod(N,2)); </langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">evenp(n);
oddp(n);</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">-- MAXScript : Even or Odd : N.H. 2019
-- Open the MAXScript Listener for input and output
userInt = getKBValue prompt:"Enter an integer and i will tell you if its Even or Odd : "
Line 2,489 ⟶ 2,942:
else if (Mod userInt 2) == 0 Then Print "Your number is even"
else Print "Your number is odd"
</syntaxhighlight>
</lang>
 
=={{header|Mercury}}==
Mercury's 'int' module provides tests for even/odd, along with all the operators that would be otherwise used to implement them.
<langsyntaxhighlight Mercurylang="mercury">even(N) % in a body, suceeeds iff N is even.
odd(N). % in a body, succeeds iff N is odd.
 
Line 2,502 ⟶ 2,955:
even(N) :- N mod 2 = 0. % using division that truncates towards -infinity
even(N) :- N rem 2 = 0. % using division that truncates towards zero
even(N) :- N /\ 1 = 0. % using bit-wise and.</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">3 even?
4 even?
5 odd?
get-stack print</langsyntaxhighlight>
{{out}}
<pre>
Line 2,516 ⟶ 2,969:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">for i in range(-4, 4)
if i % 2 == 0 then print i + " is even" else print i + " is odd"
end for</langsyntaxhighlight>
{{out}}
<pre>-4 is even
Line 2,532 ⟶ 2,985:
=={{header|MIPS Assembly}}==
This uses bitwise AND
<langsyntaxhighlight lang="mips">
.data
even_str: .asciiz "Even"
Line 2,541 ⟶ 2,994:
li $v0,5
syscall
 
#perform bitwise AND and store in $a0
and $a0,$v0,1
 
#set syscall to print dytomh
li $v0,4
 
#jump to odd if the result of the AND operation
beq $a0,1,odd
even:
#load even_str message, and print
la $a0,even_str
syscall
 
#exit program
li $v0,10
syscall
 
odd:
#load odd_str message, and print
la $a0,odd_str
syscall
 
#exit program
li $v0,10
syscall
</syntaxhighlight>
</lang>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text">/ 2 {x} ЗН</langsyntaxhighlight>
 
''Result:'' "0" - number is even; "1" - number is odd.
 
=={{header|ML}}==
<syntaxhighlight lang="ml">
<lang ml>
fun even( x: int ) = (x mod 2 = 0);
fun odd( x: int ) = (x mod 2 = 1);
</syntaxhighlight>
</lang>
==={{header|mLite}}===
<syntaxhighlight lang ="ocaml">fun odd
(x rem 2 = 1) = true
| _ = false
;
 
fun even
(x rem 2 = 0) = true
| _ = false
;
 
</syntaxhighlight>
</lang>
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE EvenOrOdd;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,ReadChar;
Line 2,607 ⟶ 3,060:
 
ReadChar
END EvenOrOdd.</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">def isEven(n)
if ((n % 2) = 1)
return false
Line 2,625 ⟶ 3,078:
println " is odd."
end
end</langsyntaxhighlight>
{{out}}
<pre>1 is odd.
Line 2,639 ⟶ 3,092:
 
=={{header|Neko}}==
<langsyntaxhighlight lang="neko">var number = 6;
 
if(number % 2 == 0) {
Line 2,645 ⟶ 3,098:
} else {
$print("Odd");
}</langsyntaxhighlight>
 
{{out}}
Line 2,652 ⟶ 3,105:
=={{header|NESL}}==
NESL provides <tt>evenp</tt> and <tt>oddp</tt> functions, but they wouldn't be hard to reimplement.
<langsyntaxhighlight lang="nesl">function even(n) = mod(n, 2) == 0;
 
% test the function by applying it to the first ten positive integers: %
{even(n) : n in [1:11]};</langsyntaxhighlight>
{{out}}
<pre>it = [F, T, F, T, F, T, F, T, F, T] : [bool]</pre>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,703 ⟶ 3,156:
else sv = 'Odd'
return sv.left(4)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,722 ⟶ 3,175:
 
=={{header|Never}}==
<syntaxhighlight lang="never">
<lang Never>
func isOdd(n : int) -> int {
n % 2 == 1
Line 2,730 ⟶ 3,183:
n % 2 == 0
}
</syntaxhighlight>
</lang>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">(odd? 1)
(even? 2)</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim"># Least signficant bit:
proc isOdd(i: int): bool = (i and 1) != 0
proc isEven(i: int): bool = (i and 1) == 0
Line 2,750 ⟶ 3,203:
 
echo isEven(1)
echo isOdd2(5)</langsyntaxhighlight>
 
=={{header|Oberon-2}}==
{{works with|oo2c}}
<langsyntaxhighlight lang="oberon2">
MODULE EvenOrOdd;
IMPORT
S := SYSTEM,
Out;
Line 2,765 ⟶ 3,218:
BEGIN
x := 10;Out.Int(x,0);
IF ODD(x) THEN Out.String(" odd") ELSE Out.String(" even") END;
Out.Ln;
 
x := 11;s := S.VAL(SET,LONG(x));Out.Int(x,0);
IF 0 IN s THEN Out.String(" odd") ELSE Out.String(" even") END;
Out.Ln;
 
x := 12;Out.Int(x,0);
Line 2,776 ⟶ 3,229:
Out.Ln
END EvenOrOdd.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,785 ⟶ 3,238:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">a := Console->ReadString()->ToInt();
if(a % 2 = 0) {
"even"->PrintLine();
Line 2,791 ⟶ 3,244:
else {
"odd"->PrintLine();
};</langsyntaxhighlight>
 
=={{header|OCaml}}==
Modulo:
<langsyntaxhighlight lang="ocaml">let is_even d =
(d mod 2) = 0
 
let is_odd d =
(d mod 2) <> 0</langsyntaxhighlight>
Bitwise and:
<langsyntaxhighlight lang="ocaml">let is_even d =
(d land 1) = 0
 
let is_odd d =
(d land 1) <> 0</langsyntaxhighlight>
 
An instructive view on functional programming and recursion:
<langsyntaxhighlight lang="ocaml">(* hmm, only valid for N >= 0 *)
let rec myeven = function
| 0 -> true
| 1 -> false
Line 2,815 ⟶ 3,268:
 
(* and here we have the not function in if form *)
let myodd n = if myeven n then false else true</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">12 isEven
12 isOdd</langsyntaxhighlight>
 
=={{header|Ol}}==
Actually, 'even?' and 'odd?' functions are built-in. But,
 
<langsyntaxhighlight lang="scheme">
; 1. Check the least significant bit.
(define (even? i)
Line 2,859 ⟶ 3,312:
(print (if (odd? 1234567898765432) "odd" "even")) ; ==> even
 
</syntaxhighlight>
</lang>
 
=={{header|OOC}}==
<langsyntaxhighlight lang="ooc">
// Using the modulo operator
even: func (n: Int) -> Bool {
Line 2,872 ⟶ 3,325:
(n & 1) == 1
}
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
GP does not have a built-in predicate for testing parity, but it's easy to code:
<langsyntaxhighlight lang="parigp">odd(n)=n%2;</langsyntaxhighlight>
Alternately:
<langsyntaxhighlight lang="parigp">odd(n)=bitand(n,1);</langsyntaxhighlight>
PARI can use the same method as [[#C|C]] for testing individual words. For multiprecision integers (t_INT), use <code>mpodd</code>. If the number is known to be nonzero, <code>mod2</code> is (insignificantly) faster.
 
=={{header|Pascal}}==
Built-in boolean function odd:
<langsyntaxhighlight lang="pascal">isOdd := odd(someIntegerNumber);</langsyntaxhighlight>
bitwise and:
<langsyntaxhighlight lang="pascal">function isOdd(Number: integer): boolean
begin
isOdd := boolean(Number and 1)
end;</langsyntaxhighlight>
Dividing and multiplying by 2 and test on equality:
<langsyntaxhighlight lang="pascal">function isEven(Number: integer): boolean
begin
isEven := (Number = ((Number div 2) * 2))
end;</langsyntaxhighlight>
Using built-in modulo
<langsyntaxhighlight lang="pascal">function isOdd(Number: integer): boolean
begin
isOdd := boolean(Number mod 2)
end;</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">for(0..10){
print "$_ is ", qw(even odd)[$_ % 2],"\n";
}</langsyntaxhighlight>
or
<langsyntaxhighlight lang="perl">print 6 % 2 ? 'odd' : 'even'; # prints even</langsyntaxhighlight>
 
=={{header|Phix}}==
There are builtin routines odd() and even() which return true/false - note however they will round non-integer arguments to the nearest whole number, which might be confusing. The mpz_odd() and mpz_even() are similar, but without any way to pass them fractions. In fact odd() invokes and_bits(i,1)=1 and even() invokes and_bits(i,1)=0, so no difference there, and "i&&1" is just shorthand for and_bits(i,1). Lastly remainder(i,2) can also validly be used, however "true" for odd numbers is actually 1 for positive odd integers and -1 for negative odd integers, plus fractions are preserved, so "remainder(i,2)==0" is perhaps for some uses a more flexible and accurate "even"/"not even" test.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 2,918 ⟶ 3,371:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d: %5t %5t %3d %5d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">odd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">even</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">),</span><span style="color: #000000;">i</span><span style="color: #0000FF;">&&</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,936 ⟶ 3,389:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">-5 5 2 tolist for
dup print " " print 2 mod if "Odd" else "Even" endif print nl
endfor</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">
// using bitwise and to check least significant digit
echo (2 & 1) ? 'odd' : 'even';
Line 2,949 ⟶ 3,402:
echo (3 % 2) ? 'odd' : 'even';
echo (4 % 2) ? 'odd' : 'even';
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,956 ⟶ 3,409:
odd
even</pre>
 
=={{header|Picat}}==
{{works with|Picat}}
<syntaxhighlight lang="picat">
% 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.
</syntaxhighlight>
{{out}}
<pre>
2 is even? Yes
2 is even? Yes
2 is even? Yes
3 is even? No
3 is even? No
3 is even? No
</pre>
 
Note: Picat has even/1 and odd/1 as built-ins predicates.
 
=={{header|PicoLisp}}==
PicoLisp doesn't have a built-in predicate for that. Using '[http://software-lab.de/doc/refB.html#bit? bit?]' is the easiest and most efficient. The bit test with 1 will return NIL if the number is even.
<langsyntaxhighlight PicoLisplang="picolisp">: (bit? 1 3)
-> 1 # Odd
 
: (bit? 1 4)
-> NIL # Even</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight Pikelang="pike">> int i = 73;
> (i&1);
Result: 1
> i%2;
Result: 1</langsyntaxhighlight>
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">i = iand(i,1)</langsyntaxhighlight>
The result is 1 when i is odd, and 0 when i is even.
 
Line 2,980 ⟶ 3,468:
at all.
 
<langsyntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN, ARG); DECLARE FN BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
Line 2,998 ⟶ 3,486:
 
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre>0 IS EVEN
Line 3,013 ⟶ 3,501:
=={{header|Plain English}}==
The noodle comes with even and odd deciders.
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
If 56 is even, write "56 is even!" to the console.
If 4 is odd, write "4 is odd!" to the console.
Wait for the escape key.
Shut down.</langsyntaxhighlight>
{{out}}
<pre>
Line 3,028 ⟶ 3,516:
===Predicate===
A predicate can be used with BigInteger objects. Even/odd predicates to not exist for basic value types. Type accelerator [bigint] can be used in place of [System.Numerics.BigInteger].
<syntaxhighlight lang="powershell">
<lang PowerShell>
$IsOdd = -not ( [bigint]$N ).IsEven
$IsEven = ( [bigint]$N ).IsEven
</syntaxhighlight>
</lang>
===Least significant digit===
<syntaxhighlight lang="powershell">
<lang PowerShell>
$IsOdd = [boolean]( $N -band 1 )
$IsEven = [boolean]( $N -band 0 )
</syntaxhighlight>
</lang>
===Remainder===
Despite being known as a modulus operator, the % operator in PowerShell actually returns a remainder. As such, when testing negative numbers it returns the true modulus result minus M. In this specific case, it returns -1 for odd negative numbers. Thus we test for not zero for odd numbers.
<syntaxhighlight lang="powershell">
<lang PowerShell>
$IsOdd = $N % 2 -ne 0
$IsEven = $N % 2 -eq 0
</syntaxhighlight>
</lang>
 
=={{header|Processing}}==
<syntaxhighlight lang="processing">
<lang Processing>
boolean isEven(int i){
return i%2 == 0;
Line 3,053 ⟶ 3,541:
return i%2 == 1;
}
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Line 3,059 ⟶ 3,547:
to test whether the integer N is even. To illustrate, here is a predicate that can
be used both to test whether an integer is even and to generate the non-negative even numbers:
<langsyntaxhighlight lang="prolog">
even(N) :-
(between(0, inf, N); integer(N) ),
0 is N mod 2.
</syntaxhighlight>
</lang>
===Least Significant Bit===
If N is a positive integer, then lsb(N) is the offset of its least significant bit, so we could write:
<langsyntaxhighlight lang="prolog">
odd(N) :- N = 0 -> false; 0 is lsb(abs(N)).
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>;use last bit method
isOdd = i & 1 ;isOdd is non-zero if i is odd
isEven = i & 1 ! 1 ;isEven is non-zero if i is even
 
;use modular method
isOdd = i % 2 ;isOdd is non-zero if i is odd
isEven = i % 2 ! 1 ;isEven is non-zero if i is even</lang>
 
=={{header|Python}}==
===Python: Using the least-significant bit method===
<langsyntaxhighlight lang="python">>>> def is_odd(i): return bool(i & 1)
 
>>> def is_even(i): return not is_odd(i)
Line 3,089 ⟶ 3,568:
>>> [(j, is_even(j)) for j in range(10)]
[(0, True), (1, False), (2, True), (3, False), (4, True), (5, False), (6, True), (7, False), (8, True), (9, False)]
>>> </langsyntaxhighlight>
 
===Python: Using modular congruences===
<langsyntaxhighlight lang="python">>> def is_even(i):
return (i % 2) == 0
 
Line 3,099 ⟶ 3,578:
>>> is_even(2)
True
>>></langsyntaxhighlight>
 
=={{header|Quackery}}==
<langsyntaxhighlight lang="quackery">[ 1 & ] is odd ( n --> b )
 
[ odd not ] is even ( n --> b )</langsyntaxhighlight>
{{out}}
In the Quackery shell (REPL):
Line 3,123 ⟶ 3,602:
See also [https://rosettacode.org/wiki/Mutual_recursion#Quackery Mutual recursion#Quackery] and [https://rosettacode.org/wiki/Anonymous_recursion#Quackery Anonymous recursion#Quackery].
 
<langsyntaxhighlight Quackerylang="quackery"> [ abs
 
' [ dup 0 = iff
[ 2drop true ] done
1 - this swap rot do ] ( x n --> b )
 
' [ dup 0 = iff
[ 2drop false ] done
1 - this swap rot do ] ( x n --> b )
 
unrot do ] is even ( n --> b )
 
11 times
[ i^ 5 - dup echo
say " is "
even iff [ $ "even" ]
else [ $ "odd" ]
echo$ say "." cr ] </langsyntaxhighlight>
 
{{out}}
Line 3,157 ⟶ 3,636:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">is.even <- function(x) !is.odd(x)
 
is.odd <- function(x) intToBits(x)[1] == 1
#or
is.odd <- function(x) x %% 2 == 1</langsyntaxhighlight>
 
=={{header|Racket}}==
With built in predicates:
<langsyntaxhighlight Racketlang="racket">(even? 6) ; -> true
(even? 5) ; -> false
(odd? 6) ; -> false
(odd? 5) ; -> true </langsyntaxhighlight>
 
With modular arithmetic:
<langsyntaxhighlight Racketlang="racket">(define (my-even? x)
(= (modulo x 2) 0))
 
(define (my-odd? x)
(= (modulo x 2) 1))</langsyntaxhighlight>
 
With mutually recursive functions:
<syntaxhighlight lang="racket">
(define (even-or-odd? i)
(letrec ([even? (λ (n)
(if (= n 0)
'even
(odd? (sub1 n))))]
[odd? (λ (n)
(if (= n 0)
'odd
(even? (sub1 n))))])
(even? i)))
 
(even-or-odd? 100) ; => 'even
(even-or-odd? 101) ; => 'odd
</syntaxhighlight>
 
=={{header|Raku}}==
(formerly Perl 6)
Raku doesn't have a built-in for this, but with subsets it's easy to define a predicate for it.
<syntaxhighlight lang="raku" perl6line>subset Even of Int where * %% 2;
subset Odd of Int where * % 2;
 
say 1 ~~ Even; # false
say 1 ~~ Odd; # true
say 1.5 ~~ Odd # false ( 1.5 is not an Int )</langsyntaxhighlight>
 
=={{header|Rascal}}==
<langsyntaxhighlight lang="rascal">public bool isEven(int n) = (n % 2) == 0;
public bool isOdd(int n) = (n % 2) == 1;</langsyntaxhighlight>
Or with block quotes:
<langsyntaxhighlight lang="rascal">public bool isEven(int n){return (n % 2) == 0;}
public bool isOdd(int n){return (n % 2) == 1;}</langsyntaxhighlight>
 
=={{header|RATFOR}}==
<syntaxhighlight lang="ratfor">
program evenodd
 
integer a
 
write(*,101,ADVANCE="NO")
read(*,102)a
 
if (mod(a,2) .eq. 0) write(*,103)a
else write(*,104)a
 
 
101 format("Enter a number: ")
102 format(i7)
103 format(i7," Is Even.")
104 format(i7," Is Odd.")
 
 
end
</syntaxhighlight>
 
=={{header|Rapira}}==
<syntaxhighlight lang="rapira">fun is_even(n)
return (n /% 2) = 0
end</syntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight lang="red">Red [
date: 2021-10-24
red-version: 0.6.4
Line 3,202 ⟶ 3,725:
 
print even? 10 ;== true
print odd? 10 ;== false</langsyntaxhighlight>
 
=={{header|ReScript}}==
 
<langsyntaxhighlight lang="rescript">let is_even = d => mod(d, 2) == 0
 
let is_odd = d => mod(d, 2) != 0</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 3,214 ⟶ 3,737:
:::* by removing a superfluous leading &nbsp; '''+''' &nbsp; sign
:::* by removing superfluous leading zeroes
:::* by removing superfluous trailing zeroes
:::* by removing a trailing decimal point
:::* possible converting an exponentiated number
:::* possible rounding the number to the current ''digits''
 
'''Programming note''': &nbsp; the last method is the fastest method in REXX to determine oddness/evenness.
<br>It requires a sparse stemmed array &nbsp; &nbsp; '''!.''' &nbsp; &nbsp; be defined in the program's prologue (or elsewhere).
<br>This method gets its speed from &nbsp; ''not'' &nbsp; using any BIF and &nbsp; ''not'' &nbsp; performing any (remainder) division.
 
'''Some notes on programming styles''': &nbsp;
If (execution) speed isn't an issue, then the 1<sup>st</sup> test method
<br>shown would be the simplest &nbsp; (in terms of coding the concisest/tightest/smallest code). &nbsp; The other test
<br>methods differ mostly in programming techniques, mostly depending on the REXX programmer's style. &nbsp;
<br>The last method shown is the fastest algorithm, albeit it might be a bit obtuse (without comments) to a
<br>novice reader of the REXX language &nbsp; (and it requires additional REXX statement baggage).
<langsyntaxhighlight lang="rexx">/*REXX program tests and displays if an integer is even or odd using different styles.*/
!.=0; do j=0 by 2 to 8; !.j=1; end /*assign 0,2,4,6,8 to a "true" value.*/
/* [↑] assigns even digits to "true".*/
Line 3,304 ⟶ 3,827:
odd: return arg(1)//2 /*returns "oddness" of the argument. */
tell: say; say center('using the' arg(1), 79, "═"); return
terr: say; say '***error***'; say; say arg(1); say; exit 13</langsyntaxhighlight>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 0 </tt>
<pre>
Line 3,361 ⟶ 3,884:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
size = 10
for i = 1 to size
Line 3,367 ⟶ 3,890:
else see "" + i + " is even" + nl ok
next
</syntaxhighlight>
</lang>
 
=={{header|RubyRPL}}==
To test oddity of real numbers (floating point numbers) :
≪ 2 MOD ≫ ‘ODD?’ STO
To test oddity of binary integers (unsigned integers) :
≪ #1 AND #1 ≠ ≫ ‘BODD?’ STO
To test oddity without caring of the data type :
≪ IF DUP TYPE THEN #1 AND #1 ≠ ELSE 2 MOD END ≫
{{in}}
<pre>
47 ODD?
#Fh BODD?
</pre>
{{out}}
<pre>
2: 1
1: 1
</pre>
 
=={{header|Ruby}}==
<lang ruby>print "evens: "
In Ruby, integers behave like objects, so they respond to methods like : 5.even? resulting to false
<syntaxhighlight lang="ruby">print "evens: "
p -5.upto(5).select(&:even?)
print "odds: "
p -5.upto(5).select(&:odd?)</langsyntaxhighlight>
{{out}}
<pre>evens: [-4, -2, 0, 2, 4]
odds: [-5, -3, -1, 1, 3, 5]</pre>
Other ways to test even-ness:
<langsyntaxhighlight lang="ruby">n & 1 == 0
quotient, remainder = n.divmod(2); remainder == 0
 
Line 3,388 ⟶ 3,929:
# You can use the bracket operator to access the i'th bit
# of a Fixnum or Bignum (i = 0 means least significant bit)
n[0].zero?</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<lang runbasic>for i = 1 to 10
if i and 1 then print i;" is odd" else print i;" is even"
next i</lang>
<pre>
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
</pre>
 
=={{header|Rust}}==
Checking the least significant digit:
<langsyntaxhighlight lang="rust">let is_odd = |x: i32| x & 1 == 1;
let is_even = |x: i32| x & 1 == 0;</langsyntaxhighlight>
 
Using modular congruences:
<langsyntaxhighlight lang="rust">let is_odd = |x: i32| x % 2 != 0;
let is_even = |x: i32| x % 2 == 0;</langsyntaxhighlight>
 
=={{header|S-BASIC}}==
S-BASIC lacks a MOD operator but supports bitwise operations on integer variables, so that is the approach taken.
<lang basic>
rem - return true (-1) if even, otherwise false (0)
function even(i = integer) = integer
var one = integer rem - both operands must be variables
one = 1
end = ((i and one) = 0)
 
rem - exercise the function
var i = integer
for i = 1 to 10 step 3
print i; " is ";
if even(i) then
print "even"
else
print "odd"
next
 
end</lang>
{{out}}
<pre>
1 is odd
4 is even
7 is odd
10 is even</pre>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">def isEven( v:Int ) : Boolean = v % 2 == 0
def isOdd( v:Int ) : Boolean = v % 2 != 0</langsyntaxhighlight>
Accept any numeric type as an argument:
<langsyntaxhighlight lang="scala">def isEven( v:Number ) : Boolean = v.longValue % 2 == 0
def isOdd( v:Number ) : Boolean = v.longValue % 2 != 0</langsyntaxhighlight>
{{out}}
<pre>isOdd( 81 ) // Results in true
Line 3,456 ⟶ 3,953:
=={{header|Scheme}}==
<code>even?</code> and <code>odd?</code> functions are built-in (R<sup>4</sup>RS, R<sup>5</sup>RS, and R<sup>6</sup>RS):
<langsyntaxhighlight lang="scheme">> (even? 5)
#f
> (even? 42)
Line 3,463 ⟶ 3,960:
#t
> (odd? 42)
#f</langsyntaxhighlight>
 
=={{header|sed}}==
<syntaxhighlight lang="sed">s/[02468]$/& is even/
s/[13579]$/& is odd/</syntaxhighlight>
{{out}}
<pre>$ seq -18 7 17 | sed -f even_or_odd.sed
-18 is even
-11 is odd
-4 is even
3 is odd
10 is even
17 is odd</pre>
 
=={{header|Seed7}}==
Test whether an integer or bigInteger is odd:
<syntaxhighlight lang ="seed7">odd(aNumber)</langsyntaxhighlight>
Test whether an integer or bigInteger is even:
<syntaxhighlight lang ="seed7">not odd(aNumber)</langsyntaxhighlight>
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">
set num to random of 100 -- start with a random number from 1 to 100
 
Line 3,483 ⟶ 3,992:
// check to see if the remainder is 0 when dividing by 2
if num rem 2 is 0 then put num & " is even (zero remainder)"
</syntaxhighlight>
</lang>
 
=={{header|SequenceL}}==
<langsyntaxhighlight lang="sequencel">even(x) := x mod 2 = 0;
odd(x) := x mod 2 = 1;</langsyntaxhighlight>
 
{{out}}
Line 3,499 ⟶ 4,008:
=={{header|SETL}}==
SETL provides built-in <tt>even</tt> and <tt>odd</tt> functions. This short program illustrates their use.
<langsyntaxhighlight lang="setl">xs := {1..10};
evens := {x in xs | even( x )};
odds := {x in xs | odd( x )};
print( evens );
print( odds );</langsyntaxhighlight>
{{out}}
<pre>{2 4 6 8 10}
Line 3,511 ⟶ 4,020:
 
Mutual Recursion:
<langsyntaxhighlight lang="shen">(define even?
0 -> true
X -> (odd? (- X 1)))
Line 3,517 ⟶ 4,026:
(define odd?
0 -> false
X -> (even? (- X 1)))</langsyntaxhighlight>
 
Modulo:
<langsyntaxhighlight lang="shen">(define even? X -> (= 0 (shen.mod X 2)))
 
(define odd? X -> (not (= 0 (shen.mod X 2))))</langsyntaxhighlight>
 
=={{header|Sidef}}==
Built-in methods:
<langsyntaxhighlight lang="ruby">var n = 42;
say n.is_odd; # false
say n.is_even; # true</langsyntaxhighlight>
 
Checking the last significant digit:
<langsyntaxhighlight lang="ruby">func is_odd(n) { n&1 == 1 };
func is_even(n) { n&1 == 0 };</langsyntaxhighlight>
 
Using modular congruences:
<langsyntaxhighlight lang="ruby">func is_odd(n) { n%2 == 1 };
func is_even(n) { n%2 == 0 };</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
Line 3,542 ⟶ 4,051:
Using the built in methods on Number class:
 
<langsyntaxhighlight lang="smalltalk">5 even
5 odd</langsyntaxhighlight>
 
even is implemented as follows:
<langsyntaxhighlight lang="smalltalk">Number>>even
^((self digitAt: 1) bitAnd: 1) = 0
</syntaxhighlight>
</lang>
 
=={{header|SNOBOL4}}==
Line 3,554 ⟶ 4,063:
{{works with|Spitbol}}
{{Works with|SNOBOL4+}}
<langsyntaxhighlight SNOBOL4lang="snobol4"> DEFINE('even(n)') :(even_end)
even even = (EQ(REMDR(n, 2), 0) 'even', 'odd') :(RETURN)
even_end
Line 3,563 ⟶ 4,072:
OUTPUT = "1 is " even(1)
OUTPUT = "2 is " even(2)
END</langsyntaxhighlight>
{{output}}<pre>-2 is even
-1 is odd
Line 3,572 ⟶ 4,081:
 
=={{header|SNUSP}}==
<syntaxhighlight lang="snusp">
<lang SNUSP>
$====!/?\==even#
- -
#odd==\?/
</syntaxhighlight>
</lang>
 
=={{header|SPL}}==
<langsyntaxhighlight lang="spl">> n, 0..9
? #.even(n), #.output(n," even")
? #.odd(n), #.output(n," odd")
<</langsyntaxhighlight>
{{out}}
<pre>
Line 3,599 ⟶ 4,108:
=={{header|SQL}}==
Database vendors can't agree on how to get a remainder. This should work for many, including Oracle. For others, including MS SQL Server, try "int % 2" instead of "mod(int, 2)".
<langsyntaxhighlight lang="sql">-- Setup a table with some integers
create table ints(int integer);
insert into ints values (-1);
Line 3,611 ⟶ 4,120:
case mod(int, 2) when 0 then 'Even' else 'Odd' end
from
ints;</langsyntaxhighlight>
 
{{out}}
Line 3,628 ⟶ 4,137:
 
For larger positive or smaller negative values of <math>n</math>, you should be ready with something else to do while the machine is working: a test run took several minutes to confirm that 32,769 was odd.
<langsyntaxhighlight lang="ssem">11110000000000100000000000000000 0. -15 to c
00000000000000110000000000000000 1. Test
11110000000001100000000000000000 2. c to 15
Line 3,642 ⟶ 4,151:
00000000000001110000000000000000 12. Stop
10000000000000000000000000000000 13. 1
01000000000000000000000000000000 14. 2</langsyntaxhighlight>
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">fun even n =
n mod 2 = 0;
 
fun odd n =
n mod 2 <> 0;
 
Line 3,655 ⟶ 4,164:
type werd = Word.word;
 
fun evenbitw(w: werd) =
Word.andb(w, 0w2) = 0w0;
 
fun oddbitw(w: werd) =
Word.andb(w, 0w2) <> 0w0;</langsyntaxhighlight>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">mata
function iseven(n) {
return(mod(n,2)==0)
Line 3,670 ⟶ 4,179:
return(mod(n,2)==1)
}
end</langsyntaxhighlight>
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">func isEven(n:Int) -> Bool {
 
// Bitwise check
if (n & 1 != 0) {
return false
}
 
// Mod check
if (n % 2 != 0) {
Line 3,685 ⟶ 4,194:
}
return true
}</langsyntaxhighlight>
=={{header|Swift}}==
<syntaxhighlight lang="swift">
// Swift has Int.isMultiple(of:Int) -> Bool
 
var isEven: (_:Int) -> Bool = {$0.isMultiple(of: 2)}
</syntaxhighlight>
 
=={{header|Symsyn}}==
<langsyntaxhighlight lang="symsyn">
n : 23
 
Line 3,695 ⟶ 4,210:
else
'n is even' []
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
# Bitwise test is the most efficient
Line 3,706 ⟶ 4,221:
puts " # O E"
puts 24:[expr isOdd(24)],[expr isEven(24)]
puts 49:[expr isOdd(49)],[expr isEven(49)]</langsyntaxhighlight>
{{out}}
<pre>
Line 3,714 ⟶ 4,229:
</pre>
 
=={{header|TI-83 BASIC57}}==
This routine returns the remainder of the division by 2 of the number in the display register. It is therefore a kind of is_odd(x) function.
TI-83 BASIC does not have a modulus operator.
Lbl 9
<lang ti83b>
/
If fPart(.5Ans
2
Then
-
Disp "ODD
CE
Else
Int
Disp "EVEN
=
End
×
</lang>
2
=
INV SBR
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">$$ MODE TUSCRIPT
LOOP n=-5,5
x=MOD(n,2)
Line 3,735 ⟶ 4,253:
PRINT n," is odd"
ENDSELECT
ENDLOOP</langsyntaxhighlight>
{{out}}
<pre>
Line 3,752 ⟶ 4,270:
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="sh">is_even() {
<lang shell>iseven() {
[[ return $(($1%2)) -eq 0 ]] && return 01))
}</syntaxhighlight>
return 1
}</lang>
 
=={{header|Ursa}}==
<langsyntaxhighlight lang="ursa">decl int input
set input (in int console)
if (= (mod input 2) 1)
Line 3,764 ⟶ 4,281:
else
out "even" endl console
end if</langsyntaxhighlight>
Output:
<pre>123
Line 3,770 ⟶ 4,287:
 
=={{header|உயிர்/Uyir}}==
<langsyntaxhighlight lang="உயிர்/Uyiruyir">முதன்மை என்பதின் வகை எண் பணி {{
எ இன் வகை எண்{$5} = 0;
படை வகை சரம்;
Line 3,787 ⟶ 4,304:
 
முதன்மை = 0;
}};</langsyntaxhighlight>
 
=={{header|VBA}}==
<pre>
4 ways = 4 Functions :
IsEven ==> Use the even and odd predicates
IsEven2 ==> Check the least significant digit. With binary integers, i bitwise-and 1 equals 0 iff i is even
IsEven3 ==> Divide i by 2. The remainder equals 0 if i is even.
IsEven4 ==> Use modular congruences</pre>
 
<lang vb>
Option Explicit
 
Sub Main_Even_Odd()
Dim i As Long
 
For i = -50 To 48 Step 7
Debug.Print i & " : IsEven ==> " & IIf(IsEven(i), "is even", "is odd") _
& " " & Chr(124) & " IsEven2 ==> " & IIf(IsEven2(i), "is even", "is odd") _
& " " & Chr(124) & " IsEven3 ==> " & IIf(IsEven3(i), "is even", "is odd") _
& " " & Chr(124) & " IsEven4 ==> " & IIf(IsEven4(i), "is even", "is odd")
Next
End Sub
 
Function IsEven(Number As Long) As Boolean
'Use the even and odd predicates
IsEven = (WorksheetFunction.Even(Number) = Number)
End Function
 
Function IsEven2(Number As Long) As Boolean
'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.
Dim lngTemp As Long
lngTemp = CLng(Right(CStr(Number), 1))
If (lngTemp And 1) = 0 Then IsEven2 = True
End Function
 
Function IsEven3(Number As Long) As Boolean
'Divide i by 2.
'The remainder equals 0 if i is even.
Dim sngTemp As Single
sngTemp = Number / 2
IsEven3 = ((Int(sngTemp) - sngTemp) = 0)
End Function
 
Function IsEven4(Number As Long) As Boolean
'Use modular congruences
IsEven4 = (Number Mod 2 = 0)
End Function
</lang>
{{out}}
<pre>-50 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
-43 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
-36 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
-29 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
-22 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
-15 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
-8 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
-1 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
6 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
13 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
20 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
27 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
34 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
41 : IsEven ==> is odd | IsEven2 ==> is odd | IsEven3 ==> is odd | IsEven4 ==> is odd
48 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even</pre>
 
=={{header|VBScript}}==
<lang vb>
Function odd_or_even(n)
If n Mod 2 = 0 Then
odd_or_even = "Even"
Else
odd_or_even = "Odd"
End If
End Function
 
WScript.StdOut.Write "Please enter a number: "
n = WScript.StdIn.ReadLine
WScript.StdOut.Write n & " is " & odd_or_even(CInt(n))
WScript.StdOut.WriteLine
</lang>
 
{{Out}}
<pre>
C:\>cscript /nologo odd_or_even.vbs
Please enter a number: 6
6 is Even
 
C:\>cscript /nologo odd_or_even.vbs
Please enter a number: 9
9 is Odd
 
C:\>cscript /nologo odd_or_even.vbs
Please enter a number: -1
-1 is Odd
</pre>
 
=={{header|Verilog}}==
<langsyntaxhighlight Veriloglang="verilog">module main;
integer i;
 
initial begin
for (i = 1; i <= 10; i = i+1) begin
Line 3,896 ⟶ 4,317:
$finish ;
end
endmodule</langsyntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn test(n i64) {
print('Testing integer $n')
 
if n&1 == 0 {
=={{header|Visual Basic .NET}}==
print(' even')
{{trans|FreeBASIC}}
}else{
<lang vbnet>Module Module1
print(' odd')
}
 
Subif Main()n%2 == 0 {
Dimprintln(' str As Stringeven')
}else{
Dim num As Integer
Whileprintln(' True odd')
}
Console.Write("Enter an integer or 0 to finish: ")
}
str = Console.ReadLine()
If Integer.TryParse(str, num) Then
If num = 0 Then
Exit While
End If
If num Mod 2 = 0 Then
Console.WriteLine("Even")
Else
Console.WriteLine("Odd")
End If
Else
Console.WriteLine("Bad input.")
End If
End While
End Sub
 
fn main(){
End Module</lang>
test(-2)
=== BigInteger ===
test(-1)
{{Libheader|System.Numerics}}
test(0)
<lang vbnet>Imports System.Numerics
test(1)
 
test(2)
Module Module1
}</syntaxhighlight>
  Function IsOdd(bi As BigInteger) As Boolean
{{out}}
Return Not bi.IsEven
<pre>
End Function
Testing integer -2 even even
 
Testing integer -1 odd odd
Function IsEven(bi As BigInteger) As Boolean
Testing integer 0 even even
Return bi.IsEven
Testing integer 1 odd odd
End Function
Testing integer 2 even even
 
</pre>
Sub Main()
' uncomment one of the following Dim statements
' Dim x As Byte = 3
' Dim x As Short = 3
' Dim x As Integer = 3
' Dim x As Long = 3
' Dim x As SByte = 3
' Dim x As UShort = 3
' Dim x As UInteger = 3
' Dim x As ULong = 3
' Dim x as BigInteger = 3
' the following three types give a warning, but will work
' Dim x As Single = 3
' Dim x As Double = 3
' Dim x As Decimal = 3
 
Console.WriteLine("{0} {1}", IsOdd(x), IsEven(x))
End Sub
End Module</lang>
 
=={{header|WDTE}}==
<langsyntaxhighlight WDTElang="wdte">let s => import 'stream';
let str => import 'strings';
 
Line 3,973 ⟶ 4,367:
-> s.map (@ s n => str.format '{} is {}.' n (evenOrOdd n))
-> s.map (io.writeln io.stdout)
-> s.drain;</langsyntaxhighlight>
 
=={{header|WebAssembly}}==
This solution tests the low bit of the given integer, which is always 0 for even numbers and 1 for odd numbers (including negative numbers).
 
<langsyntaxhighlight WebAssemblylang="webassembly">(module
;; function isOdd: returns 1 if its argument is odd, 0 if it is even.
(func $isOdd (param $n i32) (result i32)
Line 3,986 ⟶ 4,380:
)
(export "isOdd" (func $isOdd))
)</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var isEven1 = Fn.new { |i| i & 1 == 0 }
Line 4,001 ⟶ 4,395:
System.print("Method 1 : %(Fmt.v("s", -4, res1, 0, " ", ""))")
var res2 = tests.map { |t| isEven2.call(t) ? "even" : "odd" }.toList
System.print("Method 2 : %(Fmt.v("s", -4, res2, 0, " ", ""))")</langsyntaxhighlight>
 
{{out}}
Line 4,011 ⟶ 4,405:
 
=={{header|x86-64 Assembly}}==
<langsyntaxhighlight lang="x86_64 Assemblyassembly">
evenOdd:
mov rax,1
and rax,rdi
ret
</syntaxhighlight>
</lang>
 
=={{header|XBS}}==
<syntaxhighlight lang="xbs">#>
<lang XBS>#>
Typed XBS
evenOrOdd function
Line 4,031 ⟶ 4,425:
foreach(v of arr){
log(v+" is even? "+evenOrOdd(v))
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,047 ⟶ 4,441:
 
=={{header|xEec}}==
<syntaxhighlight lang="xeec">
<lang xEec>
>100 p i# jz-1 o# t h#1 ms jz2003 p >0110 h#2 r ms t h#1 ms p
jz1002 h? jz2003 p jn0110 h#10 o$ p jn100 >2003 p p h#0 h#10
h$d h$d h$o h#32 h$s h$i h#32 jn0000 >1002 p p h#0 h#10
h$n h$e h$v h$e h#32 h$s h$i h#32 >0000 o$ p jn0000 jz100
</syntaxhighlight>
</lang>
 
=={{header|XLISP}}==
XLISP provides <tt>EVENP</tt> and <tt>ODDP</tt>, or, if you prefer, <tt>EVEN?</tt> and <tt>ODD?</tt>; if one wanted to reimplement them, it could be done like this (or in other ways).
<langsyntaxhighlight lang="lisp">(defun my-evenp (x)
(= (logand x 1) 0) )
 
(defun my-oddp (x)
(/= (logand x 1) 0) )</langsyntaxhighlight>
 
=={{header|Xojo}}==
<syntaxhighlight lang="vb">
<lang vb>
For num As Integer = 1 To 5
If num Mod 2 = 0 Then
Line 4,071 ⟶ 4,465:
End If
Next
</syntaxhighlight>
</lang>
 
{{Out}}
Line 4,083 ⟶ 4,477:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">include c:\cxpl\codes;
int I;
[for I:= -4 to +3 do
[IntOut(0, I);
Text(0, if I&1 then " is odd " else " is even ");
Text(0, if rem(I/2)#0 then "odd" else "even");
CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 4,104 ⟶ 4,498:
3 is odd odd
</pre>
 
=={{header|Yabasic}}==
{{trans|Phix}}
<lang Yabasic>for i = -5 to 5
print i, and(i,1), mod(i,2)
next
</lang>
 
=={{header|Z80 Assembly}}==
Z80 Assembly has a few ways of testing if a number is even or odd:
===RRC===
A right rotate will set the carry if the register's value is odd and clear it if it's even. This does alter the contents of the register, so only use this method if you don't need to remember the number being tested after getting the results of the test. This is the fastest way the Z80 can test a value for even or odd, but only when testing the accumulator <tt>A</tt>
<langsyntaxhighlight lang="z80">rrca
jp nc,isEven</langsyntaxhighlight>
 
===SRA/SRL===
In similar vein, there are also shift instructions. The arithmetic shift instruction retains the sign bit (bit 7) of the operand in question, while the logical shift sets bit 7 to 0.
<syntaxhighlight lang="z80">sra a
jp nc,isEven</syntaxhighlight>
 
===AND===
<code>AND 1</code> will change the accumulator to 1 if its value was odd, and 0 if its value was even. If you want to selectively load a 0 or 1 into the accumulator based on whether a variable is odd or even, this is the best way to do so. Like the <code>RRC</code> method, this test is destructive, so if you need to preserve the original value of the accumulator after the test, use the method below instead.
<langsyntaxhighlight lang="z80">and &01
jp z,isEven</langsyntaxhighlight>
 
===BIT===
This method is the slowest, but it doesn't change the value in the register being tested. It works on any 8 bit register, (HL), (IX+#), or (IY+#), making it the most versatile. Although I say it's the slowest, the difference is so small and the execution time so fast that you'd never notice anyway. The Z80 can perform all these tests faster than you can blink!
<langsyntaxhighlight lang="z80">bit 0,c
jp z,C_IS_EVEN</langsyntaxhighlight>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">[-3..4].pump(fcn(n){ println(n," is ",n.isEven and "even" or "odd") })</langsyntaxhighlight>
Ints have isEven and isOdd properties. pump, in this case, is the same as apply/map without aggregating a result.
{{out}}
Line 4,143 ⟶ 4,535:
4 is even
</pre>
<langsyntaxhighlight lang="zkl">[-3..4].apply("isEven").println();</langsyntaxhighlight>
{{out}}
<pre>L(False,True,False,True,False,True,False,True)</pre>
 
=={{header|Zoea}}==
<syntaxhighlight lang="zoea">
<lang Zoea>
program: even_or_odd
case: 1
Line 4,162 ⟶ 4,554:
input: 7
output: odd
</syntaxhighlight>
</lang>
 
=={{header|Zoea Visual}}==
Line 4,168 ⟶ 4,560:
 
=={{header|zonnon}}==
<langsyntaxhighlight lang="zonnon">
module Main;
var
Line 4,179 ⟶ 4,571:
s := set(x);writeln(x:3," is odd?",0 in s); (* check right bit *)
end Main.
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 4,187 ⟶ 4,579:
11 is odd? true
</pre>
 
=={{header|ZX Spectrum Basic}}==
<lang zxbasic>10 FOR n=-3 TO 4: GO SUB 30: NEXT n
20 STOP
30 LET odd=FN m(n,2)
40 PRINT n;" is ";("Even" AND odd=0)+("Odd" AND odd=1)
50 RETURN
60 DEF FN m(a,b)=a-INT (a/b)*b</lang>
56

edits