Even or odd: Difference between revisions

5,925 bytes added ,  2 months ago
m
 
(30 intermediate revisions by 17 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>
 
Line 784 ⟶ 793:
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>
Line 797 ⟶ 806:
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}}===
Line 874 ⟶ 893:
 
==={{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 882 ⟶ 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}}===
Line 898 ⟶ 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 927 ⟶ 954:
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}}===
Line 933 ⟶ 968:
if i and 1 then print i;" is odd" else print i;" is even"
next i</syntaxhighlight>
<pre>1 is odd
1 is odd
2 is even
3 is odd
Line 943 ⟶ 977:
8 is even
9 is odd
10 is even</pre>
</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)
Line 967 ⟶ 1,000:
end</syntaxhighlight>
{{out}}
<pre> 1 is odd
1 is odd
4 is even
7 is odd
Line 975 ⟶ 1,007:
==={{header|TI-83 BASIC}}===
TI-83 BASIC does not have a modulus operator.
<syntaxhighlight lang="ti83b">If fPart(.5Ans
If fPart(.5Ans
Then
Disp "ODD
Else
Disp "EVEN
End</syntaxhighlight>
End
</syntaxhighlight>
 
==={{header|Tiny BASIC}}===
Line 1,002 ⟶ 1,032:
 
==={{header|VBA}}===
<pre>4 ways = 4 Functions :
<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
Line 1,009 ⟶ 1,038:
IsEven4 ==> Use modular congruences</pre>
 
<syntaxhighlight lang="vb">Option Explicit
Option Explicit
 
Sub Main_Even_Odd()
Line 1,047 ⟶ 1,075:
'Use modular congruences
IsEven4 = (Number Mod 2 = 0)
End Function</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>-50 : IsEven ==> is even | IsEven2 ==> is even | IsEven3 ==> is even | IsEven4 ==> is even
Line 1,067 ⟶ 1,094:
 
==={{header|VBScript}}===
<syntaxhighlight lang="vb">Function odd_or_even(n)
Function odd_or_even(n)
If n Mod 2 = 0 Then
odd_or_even = "Even"
Line 1,079 ⟶ 1,105:
n = WScript.StdIn.ReadLine
WScript.StdOut.Write n & " is " & odd_or_even(CInt(n))
WScript.StdOut.WriteLine</syntaxhighlight>
</syntaxhighlight>
 
{{Out}}
<pre>C:\>cscript /nologo odd_or_even.vbs
<pre>
C:\>cscript /nologo odd_or_even.vbs
Please enter a number: 6
6 is Even
Line 1,094 ⟶ 1,117:
C:\>cscript /nologo odd_or_even.vbs
Please enter a number: -1
-1 is Odd</pre>
</pre>
 
==={{header|Visual Basic .NET}}===
Line 1,179 ⟶ 1,201:
==={{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 1,200 ⟶ 1,221:
 
set test
pause>nul</syntaxhighlight>
</syntaxhighlight>
 
=={{header|bc}}==
Line 1,233 ⟶ 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 1,279 ⟶ 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,290 ⟶ 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,381 ⟶ 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,394 ⟶ 1,421:
 
<syntaxhighlight lang="cpp">
template < typename T >
constexpr inline bool isEven( const T& v )
{
Line 1,400 ⟶ 1,427:
}
 
template <>
constexpr inline bool isEven< int >( const int& v )
{
Line 1,406 ⟶ 1,433:
}
 
template < typename T >
constexpr inline bool isOdd( const T& v )
{
Line 1,448 ⟶ 1,475:
(cond ((evenp nr) "even")
((oddp nr) "odd")))
(dotimes (n 10)
(if (< n 1) (terpri))
(if (< n 9) (format t "~a" " "))
Line 1,498 ⟶ 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,505 ⟶ 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,512 ⟶ 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,519 ⟶ 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,622 ⟶ 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,698 ⟶ 1,740:
10 is even
</pre>
 
=={{header|Déjà Vu}}==
<syntaxhighlight lang="dejavu">even n:
Line 1,730 ⟶ 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,788 ⟶ 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,829 ⟶ 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,844 ⟶ 1,942:
true ->
io:format("even\n")
end.
</syntaxhighlight>
===Using the least-significant bit method===
Line 1,859 ⟶ 1,957:
true ->
io:format("even\n")
end.
</syntaxhighlight>
 
Line 1,983 ⟶ 2,081:
=={{header|Forth}}==
<syntaxhighlight lang="forth">
: odd? ( n -- ? ) 1 and ;
: even? ( n -- ? ) odd? 0= ;
 
Line 2,103 ⟶ 2,201:
 
=={{header|Fōrmulæ}}==
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.
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Even_or_odd}}
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.
 
'''Solutions'''
 
'''Case 1.''' Intrinsic expressions:
 
[[File:Fōrmulæ - Even or odd 01.png]]
 
[[File:Fōrmulæ - Even or odd 02.png]]
 
'''Case 2.''' Using the Divides and DoesNotDivide expressions:
 
[[File:Fōrmulæ - Even or odd 03.png]]
 
[[File:Fōrmulæ - Even or odd 04.png]]
 
'''Case 3.''' Using modular congruences
 
[[File:Fōrmulæ - Even or odd 05.png]]
 
[[File:Fōrmulæ - Even or odd 06.png]]
 
'''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]]
In '''[https://formulae.org/?example=Even_or_odd this]''' page you can see the program(s) related to this task and their results.
 
=={{header|GAP}}==
Line 2,252 ⟶ 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,273 ⟶ 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,348 ⟶ 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,448 ⟶ 2,597:
 
<syntaxhighlight lang="scheme">
{def is_odd {lambda {:i} {= {% :i 2} 1}}}
-> is_odd
 
Line 2,702 ⟶ 2,851:
Binary.Add take any numeric type, but value from newer versions 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).
Line 2,719 ⟶ 2,872:
}
</pre >
 
The same hold for Def Odd(a)=binary.and(Abs(a), 1)=1
Interpreter execute this:
Line 2,768 ⟶ 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,841 ⟶ 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,880 ⟶ 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 3,056 ⟶ 3,209:
<syntaxhighlight lang="oberon2">
MODULE EvenOrOdd;
IMPORT
S := SYSTEM,
Out;
Line 3,065 ⟶ 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 3,109 ⟶ 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,450 ⟶ 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,498 ⟶ 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,519 ⟶ 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,538 ⟶ 3,730:
 
<syntaxhighlight lang="rescript">let is_even = d => mod(d, 2) == 0
 
let is_odd = d => mod(d, 2) != 0</syntaxhighlight>
 
Line 3,545 ⟶ 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,699 ⟶ 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,751 ⟶ 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,932 ⟶ 4,154:
 
=={{header|Standard ML}}==
<syntaxhighlight lang="sml">fun even n =
n mod 2 = 0;
 
fun odd n =
n mod 2 <> 0;
 
Line 3,942 ⟶ 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,961 ⟶ 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,973 ⟶ 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 4,000 ⟶ 4,228:
49:1,0
</pre>
 
=={{header|TI-57}}==
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.
Lbl 9
/
2
-
CE
Int
=
×
2
=
INV SBR
 
=={{header|TUSCRIPT}}==
Line 4,028 ⟶ 4,270:
 
=={{header|UNIX Shell}}==
<syntaxhighlight lang="shellsh">isevenis_even() {
[[ return $(($1%2)) -eq 0 ]] && return 01))
return 1
}</syntaxhighlight>
 
Line 4,068 ⟶ 4,309:
<syntaxhighlight lang="verilog">module main;
integer i;
 
initial begin
for (i = 1; i <= 10; i = i+1) begin
Line 4,143 ⟶ 4,384:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var isEven1 = Fn.new { |i| i & 1 == 0 }
Line 4,240 ⟶ 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,263 ⟶ 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>
 
56

edits