Even or odd: Difference between revisions

6,404 bytes added ,  2 months ago
m
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
 
(34 intermediate revisions by 19 users not shown)
Line 1:
{{task}} [[Category:Simple]]
 
;Task:
Line 78:
.en
</syntaxhighlight>
 
=={{header|68000 Assembly}}==
 
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.
 
<syntaxhighlight lang="8080asm">CMDLIN: equ 80h ; Location of CP/M command line argument
Line 121 ⟶ 122:
org 100h
lxi h,CMDLIN ; Find length of argument
mov a,m
add l ; Look up last character (digit)
mov l,a
Line 179 ⟶ 180:
<syntaxhighlight lang="forth">: odd? \ n -- boolean
dup 1 n:band 1 n:= ;
: even? \ n -- boolean
odd? not ;</syntaxhighlight>
 
Line 189 ⟶ 190:
even? not ;
</syntaxhighlight>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight 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 273 ⟶ 275:
2021 is odd (impair)
</pre>
 
=={{header|ABAP}}==
<syntaxhighlight lang="abap">
Line 339 ⟶ 342:
 
FOR i=-4 TO 4
DO
PrintF("%I is",i)
OddByAnd(i)
Line 379 ⟶ 382:
 
=={{header|Agda}}==
<syntaxhighlight 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</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Aime}}==
Line 492 ⟶ 502:
 
 
Or, packaging reusable functions that can serve as arguments to '''filter''', '''partition''' etc
(deriving '''even''' from mod, and '''odd''' from even):
<syntaxhighlight lang="applescript">----------------------- EVEN OR ODD ------------------------
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 574 ⟶ 584:
"| @input | is odd!"
}</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight 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 706 ⟶ 717:
else -> print [pad to :string x 4 ": odd"]
]</syntaxhighlight>
{{out}}
 
<pre> -5 : odd
-4 : even
Line 742 ⟶ 751:
=={{header|AWK}}==
<syntaxhighlight lang="awk">function isodd(x) {
return (x % 2) !=0; 0
}
 
function iseven(x) {
return (x % 2) ==0; 0
}</syntaxhighlight>
 
=={{header|BaConBASIC}}==
==={{header|Applesoft BASIC}}===
<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
Line 755 ⟶ 772:
n = IIF$(dim < 2, 0, VAL(arg$[1]))
PRINT n, " is ", IIF$(EVEN(n), "even", "odd")</syntaxhighlight>
 
{{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}}===
<syntaxhighlight 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</syntaxhighlight>
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}}===
Uses bitwise AND as suggested.
<syntaxhighlight lang="gwbasic">10 rem determine if integer is even or odd
20 print "Enter an integer:";
Line 782 ⟶ 826:
50 if (i% and 1)=1 then eo$="odd"
60 print "The number ";i%;"is ";eo$;"."</syntaxhighlight>
 
==={{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}}===
Line 796 ⟶ 885:
150 PRINT X;"is even."
160 END IF</syntaxhighlight>
 
==={{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}}
<syntaxhighlight 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>
 
</syntaxhighlight>
==={{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}}===
Line 813 ⟶ 925:
<syntaxhighlight lang="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 834 ⟶ 946:
Print "Still Even"
End If</syntaxhighlight>
 
==={{header|Tiny BASIC}}===
<syntaxhighlight lang="text">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."</syntaxhighlight>
 
==={{header|BASIC256}}===
{{works with|True 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|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
{{works with|QRunRun 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}}
<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."
70 END</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 864 ⟶ 1,030:
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}}===
Line 878 ⟶ 1,191:
 
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
set /p i=Insert number:
@echo off
set /p i=Insert number:
 
::bitwise and
Line 894 ⟶ 1,221:
 
set test
pause>nul</syntaxhighlight>
</syntaxhighlight>
 
=={{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.
<syntaxhighlight 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)</syntaxhighlight>
{{out}}
<pre>
14 is even
15 is odd
9876543210 is even
9876543211 is odd
</pre>
 
=={{header|bc}}==
Line 949 ⟶ 1,253:
 
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}}==
Line 995 ⟶ 1,306:
 
=={{header|Brainf***}}==
Assumes that input characters are an ASCII representation of a valid integer.
Output is input <tt>mod</tt> 2.
<syntaxhighlight lang="bf">,[>,----------] Read until newline
Line 1,006 ⟶ 1,317:
>[>+<-]>. to get n % 2 to ASCII and print</syntaxhighlight>
 
If one need only determine rather than act on the parity of the input,
the following is sufficient; it terminates either quickly or never.
<syntaxhighlight lang="bf">,[>,----------]<[--]</syntaxhighlight>
Line 1,097 ⟶ 1,408:
=={{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,110 ⟶ 1,421:
 
<syntaxhighlight lang="cpp">
template < typename T >
constexpr inline bool isEven( const T& v )
{
Line 1,116 ⟶ 1,427:
}
 
template <>
constexpr inline bool isEven< int >( const int& v )
{
Line 1,122 ⟶ 1,433:
}
 
template < typename T >
constexpr inline bool isOdd( const T& v )
{
Line 1,164 ⟶ 1,475:
(cond ((evenp nr) "even")
((oddp nr) "odd")))
(dotimes (n 10)
(if (< n 1) (terpri))
(if (< n 9) (format t "~a" " "))
Line 1,214 ⟶ 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,221 ⟶ 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,228 ⟶ 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,235 ⟶ 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)
Line 1,338 ⟶ 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}}==
Line 1,414 ⟶ 1,740:
10 is even
</pre>
 
=={{header|Déjà Vu}}==
<syntaxhighlight lang="dejavu">even n:
Line 1,446 ⟶ 1,773:
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}}==
Line 1,504 ⟶ 1,841:
<syntaxhighlight 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,545 ⟶ 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}}==
Line 1,560 ⟶ 1,942:
true ->
io:format("even\n")
end.
</syntaxhighlight>
===Using the least-significant bit method===
Line 1,575 ⟶ 1,957:
true ->
io:format("even\n")
end.
</syntaxhighlight>
 
Line 1,699 ⟶ 2,081:
=={{header|Forth}}==
<syntaxhighlight lang="forth">
: odd? ( n -- ? ) 1 and ;
: even? ( n -- ? ) odd? 0= ;
 
Line 1,788 ⟶ 2,170:
end program oe
</syntaxhighlight>
 
=={{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|Frink}}==
Line 1,820 ⟶ 2,181:
fun main(x: int): bool = (x & 1) == 0
</syntaxhighlight>
 
 
=={{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}}==
<syntaxhighlight 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</syntaxhighlight>
 
'''Case 3.''' Using modular congruences
 
[[File:Fōrmulæ - Even or odd 05.png]]
 
[[File:Fōrmulæ - Even or odd 06.png]]
Output:
 
<pre>
'''Case 4.''' Using bitwise operations
'25' is an odd number
 
'100' is an even number
[[File:Fōrmulæ - Even or odd 07.png]]
'Fred' does not compute!!
 
</pre>
[[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}}==
Line 1,996 ⟶ 2,379:
return n%2 = 0
end</syntaxhighlight>
 
=={{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}}==
Line 2,017 ⟶ 2,405:
0 1 1 1</syntaxhighlight>
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}}==
Line 2,092 ⟶ 2,493:
<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
 
Line 2,192 ⟶ 2,597:
 
<syntaxhighlight lang="scheme">
{def is_odd {lambda {:i} {= {% :i 2} 1}}}
-> is_odd
 
Line 2,248 ⟶ 2,653:
 
.END</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">n=12
 
if n mod 2 = 0 then print "even" else print "odd"</syntaxhighlight>
 
=={{header|Lingo}}==
 
<syntaxhighlight lang="lingo">on even (n)
return n mod 2 = 0
Line 2,450 ⟶ 2,849:
 
=={{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,463 ⟶ 2,872:
}
</pre >
 
The same hold for Def Odd(a)=binary.and(Abs(a), 1)=1
Interpreter execute this:
Line 2,479 ⟶ 2,888:
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,516 ⟶ 2,921:
 
=={{header|MATLAB}} / {{header|Octave}}==
Bitwise And:
<syntaxhighlight lang="matlab"> isOdd = logical(bitand(N,1));
isEven = ~logical(bitand(N,1)); </syntaxhighlight>
Remainder of division by two :
<syntaxhighlight lang="matlab"> isOdd = logical(rem(N,2));
isEven = ~logical(rem(N,2)); </syntaxhighlight>
Line 2,589 ⟶ 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
Line 2,628 ⟶ 3,033:
</syntaxhighlight>
==={{header|mLite}}===
<syntaxhighlight lang="ocaml">fun odd
(x rem 2 = 1) = true
| _ = false
;
 
fun even
(x rem 2 = 0) = true
| _ = false
;
 
</syntaxhighlight>
Line 2,804 ⟶ 3,209:
<syntaxhighlight lang="oberon2">
MODULE EvenOrOdd;
IMPORT
S := SYSTEM,
Out;
Line 2,813 ⟶ 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,857 ⟶ 3,262:
An instructive view on functional programming and recursion:
<syntaxhighlight lang="ocaml">(* hmm, only valid for N >= 0 *)
let rec myeven = function
| 0 -> true
| 1 -> false
Line 3,152 ⟶ 3,557:
odd(N) :- N = 0 -> false; 0 is lsb(abs(N)).
</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|Python}}==
Line 3,207 ⟶ 3,603:
 
<syntaxhighlight lang="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 ] </syntaxhighlight>
 
Line 3,255 ⟶ 3,651:
With modular arithmetic:
<syntaxhighlight lang="racket">(define (my-even? x)
(= (modulo x 2) 0))
 
(define (my-odd? x)
(= (modulo x 2) 1))</syntaxhighlight>
 
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}}==
Line 3,276 ⟶ 3,689:
<syntaxhighlight lang="rascal">public bool isEven(int n){return (n % 2) == 0;}
public bool isOdd(int n){return (n % 2) == 1;}</syntaxhighlight>
 
=={{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}}==
Line 3,295 ⟶ 3,730:
 
<syntaxhighlight lang="rescript">let is_even = d => mod(d, 2) == 0
 
let is_odd = d => mod(d, 2) != 0</syntaxhighlight>
 
Line 3,302 ⟶ 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).
Line 3,456 ⟶ 3,891:
next
</syntaxhighlight>
 
=={{header|RPL}}==
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}}==
Line 3,477 ⟶ 3,930:
# of a Fixnum or Bignum (i = 0 means least significant bit)
n[0].zero?</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|Rust}}==
Line 3,504 ⟶ 3,939:
<syntaxhighlight lang="rust">let is_odd = |x: i32| x % 2 != 0;
let is_even = |x: i32| x % 2 == 0;</syntaxhighlight>
 
=={{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|Scala}}==
Line 3,553 ⟶ 3,961:
> (odd? 42)
#f</syntaxhighlight>
 
=={{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}}==
Line 3,734 ⟶ 4,154:
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">fun even n =
n mod 2 = 0;
 
fun odd n =
n mod 2 <> 0;
 
Line 3,744 ⟶ 4,164:
type werd = Word.word;
 
fun evenbitw(w: werd) =
Word.andb(w, 0w2) = 0w0;
 
fun oddbitw(w: werd) =
Word.andb(w, 0w2) <> 0w0;</syntaxhighlight>
 
Line 3,763 ⟶ 4,183:
=={{header|Swift}}==
<syntaxhighlight lang="swift">func isEven(n:Int) -> Bool {
 
// Bitwise check
if (n & 1 != 0) {
return false
}
 
// Mod check
if (n % 2 != 0) {
Line 3,775 ⟶ 4,195:
return true
}</syntaxhighlight>
=={{header|Swift}}==
<syntaxhighlight lang="swift">
// Swift has Int.isMultiple(of:Int) -> Bool
 
var isEven: (_:Int) -> Bool = {$0.isMultiple(of: 2)}
</syntaxhighlight>
 
=={{header|Symsyn}}==
Line 3,803 ⟶ 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
<syntaxhighlight lang="ti83b">
/
If fPart(.5Ans
2
Then
-
Disp "ODD
CE
Else
Int
Disp "EVEN
=
End
×
</syntaxhighlight>
2
=
INV SBR
 
=={{header|TUSCRIPT}}==
Line 3,841 ⟶ 4,270:
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="shellsh">isevenis_even() {
[[ return $(($1%2)) -eq 0 ]] && return 01))
return 1
}</syntaxhighlight>
 
Line 3,877 ⟶ 4,305:
முதன்மை = 0;
}};</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|Verilog}}==
<syntaxhighlight lang="verilog">module main;
integer i;
 
initial begin
for (i = 1; i <= 10; i = i+1) begin
Line 3,986 ⟶ 4,318:
end
endmodule</syntaxhighlight>
 
 
=={{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|V (Vlang)}}==
Line 4,112 ⟶ 4,384:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var isEven1 = Fn.new { |i| i & 1 == 0 }
Line 4,209 ⟶ 4,481:
[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);
Line 4,226 ⟶ 4,498:
3 is odd odd
</pre>
 
=={{header|Yabasic}}==
{{trans|Phix}}
<syntaxhighlight lang="yabasic">for i = -5 to 5
print i, and(i,1), mod(i,2)
next
</syntaxhighlight>
 
=={{header|Z80 Assembly}}==
Line 4,239 ⟶ 4,504:
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>
<syntaxhighlight lang="z80">rrca
jp nc,isEven</syntaxhighlight>
 
===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>
 
Line 4,309 ⟶ 4,579:
11 is odd? true
</pre>
 
=={{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>
56

edits