Even or odd: Difference between revisions

4,864 bytes added ,  2 months ago
m
(→‎dc: add)
 
(26 intermediate revisions by 16 users not shown)
Line 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 389 ⟶ 395:
 
odd zero = false
odd (suc n) = even n</syntaxhighlight>
</syntaxhighlight>
 
=={{header|Aime}}==
Line 799 ⟶ 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 876 ⟶ 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
Line 884 ⟶ 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 929 ⟶ 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 935 ⟶ 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 945 ⟶ 977:
8 is even
9 is odd
10 is even</pre>
</pre>
 
==={{header|S-BASIC}}===
Line 969 ⟶ 1,000:
end</syntaxhighlight>
{{out}}
<pre> 1 is odd
1 is odd
4 is even
7 is odd
Line 977 ⟶ 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,004 ⟶ 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,011 ⟶ 1,038:
IsEven4 ==> Use modular congruences</pre>
 
<syntaxhighlight lang="vb">Option Explicit
Option Explicit
 
Sub Main_Even_Odd()
Line 1,049 ⟶ 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,069 ⟶ 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,081 ⟶ 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,096 ⟶ 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,188 ⟶ 1,208:
 
=={{header|Batch File}}==
<syntaxhighlight lang="dos">@echo off
@echo off
set /p i=Insert number:
 
Line 1,202 ⟶ 1,221:
 
set test
pause>nul</syntaxhighlight>
</syntaxhighlight>
 
=={{header|bc}}==
Line 1,235 ⟶ 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,624 ⟶ 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}}==
Line 1,737 ⟶ 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,836 ⟶ 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 2,110 ⟶ 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'''
In '''[https://formulae.org/?example=Even_or_odd this]''' page you can see the program(s) related to this task and their results.
 
'''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]]
 
=={{header|GAP}}==
Line 2,259 ⟶ 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,280 ⟶ 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,355 ⟶ 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 3,513 ⟶ 3,655:
(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,530 ⟶ 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,710 ⟶ 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,996 ⟶ 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,023 ⟶ 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,165 ⟶ 4,384:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var isEven1 = Fn.new { |i| i & 1 == 0 }
Line 4,285 ⟶ 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