Even or odd: Difference between revisions
Content added Content deleted
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
(Dialects of BASIC moved to the BASIC section.) |
||
Line 749: | Line 749: | ||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
=={{header| |
=={{header|BASIC}}== |
||
==={{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 |
<syntaxhighlight lang="freebasic">' Even or odd |
||
OPTION MEMTYPE int |
OPTION MEMTYPE int |
||
Line 755: | Line 763: | ||
n = IIF$(dim < 2, 0, VAL(arg$[1])) |
n = IIF$(dim < 2, 0, VAL(arg$[1])) |
||
PRINT n, " is ", IIF$(EVEN(n), "even", "odd")</syntaxhighlight> |
PRINT n, " is ", IIF$(EVEN(n), "even", "odd")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>prompt$ ./even-or-odd 42 |
<pre>prompt$ ./even-or-odd 42 |
||
Line 762: | Line 769: | ||
41 is odd</pre> |
41 is odd</pre> |
||
=={{header| |
==={{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|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 |
|||
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|Commodore BASIC}}=== |
==={{header|Commodore BASIC}}=== |
||
Uses bitwise AND as suggested. |
Uses bitwise AND as suggested. |
||
<syntaxhighlight lang="gwbasic">10 rem determine if integer is even or odd |
<syntaxhighlight lang="gwbasic">10 rem determine if integer is even or odd |
||
20 print "Enter an integer:"; |
20 print "Enter an integer:"; |
||
Line 782: | Line 807: | ||
50 if (i% and 1)=1 then eo$="odd" |
50 if (i% and 1)=1 then eo$="odd" |
||
60 print "The number ";i%;"is ";eo$;"."</syntaxhighlight> |
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}}=== |
==={{header|GW-BASIC}}=== |
||
Line 796: | Line 866: | ||
150 PRINT X;"is even." |
150 PRINT X;"is even." |
||
160 END IF</syntaxhighlight> |
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}}=== |
==={{header|Minimal BASIC}}=== |
||
Line 808: | Line 884: | ||
80 END |
80 END |
||
</syntaxhighlight> |
</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}}=== |
==={{header|QB64}}=== |
||
Line 834: | Line 919: | ||
Print "Still Even" |
Print "Still Even" |
||
End If</syntaxhighlight> |
End If</syntaxhighlight> |
||
==={{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|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}}=== |
==={{header|Tiny BASIC}}=== |
||
Line 842: | Line 991: | ||
50 END |
50 END |
||
60 PRINT "It's even."</syntaxhighlight> |
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|QRun 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|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
Line 864: | Line 998: | ||
NEXT i |
NEXT i |
||
END</syntaxhighlight> |
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}}=== |
==={{header|XBasic}}=== |
||
Line 878: | Line 1,167: | ||
END PROGRAM</syntaxhighlight> |
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}}== |
=={{header|Batch File}}== |
||
Line 896: | Line 1,200: | ||
pause>nul |
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}}== |
=={{header|bc}}== |
||
Line 1,788: | Line 2,070: | ||
end program oe |
end program oe |
||
</syntaxhighlight> |
</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}}== |
=={{header|Frink}}== |
||
Line 1,822: | Line 2,083: | ||
=={{header|Fōrmulæ}}== |
=={{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 —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition. |
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 —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition. |
||
Line 1,828: | Line 2,088: | ||
In '''[https://formulae.org/?example=Even_or_odd this]''' page you can see the program(s) related to this task and their results. |
In '''[https://formulae.org/?example=Even_or_odd this]''' page you can see the program(s) related to this task and their results. |
||
=={{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|GAP}}== |
=={{header|GAP}}== |
||
Line 2,248: | Line 2,484: | ||
.END</syntaxhighlight> |
.END</syntaxhighlight> |
||
=={{header|Liberty BASIC}}== |
|||
<syntaxhighlight lang="lb">n=12 |
|||
if n mod 2 = 0 then print "even" else print "odd"</syntaxhighlight> |
|||
=={{header|Lingo}}== |
=={{header|Lingo}}== |
||
<syntaxhighlight lang="lingo">on even (n) |
<syntaxhighlight lang="lingo">on even (n) |
||
return n mod 2 = 0 |
return n mod 2 = 0 |
||
Line 3,152: | Line 3,382: | ||
odd(N) :- N = 0 -> false; 0 is lsb(abs(N)). |
odd(N) :- N = 0 -> false; 0 is lsb(abs(N)). |
||
</syntaxhighlight> |
</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}}== |
=={{header|Python}}== |
||
Line 3,477: | Line 3,698: | ||
# of a Fixnum or Bignum (i = 0 means least significant bit) |
# of a Fixnum or Bignum (i = 0 means least significant bit) |
||
n[0].zero?</syntaxhighlight> |
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}}== |
=={{header|Rust}}== |
||
Line 3,504: | Line 3,707: | ||
<syntaxhighlight lang="rust">let is_odd = |x: i32| x % 2 != 0; |
<syntaxhighlight lang="rust">let is_odd = |x: i32| x % 2 != 0; |
||
let is_even = |x: i32| x % 2 == 0;</syntaxhighlight> |
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}}== |
=={{header|Scala}}== |
||
Line 3,802: | Line 3,978: | ||
49:1,0 |
49:1,0 |
||
</pre> |
</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|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
Line 3,877: | Line 4,042: | ||
முதன்மை = 0; |
முதன்மை = 0; |
||
}};</syntaxhighlight> |
}};</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}}== |
=={{header|Verilog}}== |
||
Line 3,986: | Line 4,055: | ||
end |
end |
||
endmodule</syntaxhighlight> |
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)}}== |
=={{header|V (Vlang)}}== |
||
Line 4,226: | Line 4,235: | ||
3 is odd odd |
3 is odd odd |
||
</pre> |
</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}}== |
=={{header|Z80 Assembly}}== |
||
Line 4,309: | Line 4,311: | ||
11 is odd? true |
11 is odd? true |
||
</pre> |
</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> |