Hi all,
How can I make Visual basic tell whether an integer is an odd or even number.
Thanks,
Justin Arendt
MCSE, MCP+I
Printable View
Hi all,
How can I make Visual basic tell whether an integer is an odd or even number.
Thanks,
Justin Arendt
MCSE, MCP+I
Try this:
Code:Public Function EvenOdd(num&) As Boolean
Dim a As Long
a = (Int(num& / 2)) * 2
If a = num& Then EvenOdd = True 'Even
If a <> num& Then EvenOdd = False 'Odd
End Function
Usage:
If EvenOdd(2) Then
MsgBox "Even"
Else
MsgBox "Odd"
End If
if you run this and answer turns out to be 1 then it is an odd number, if you get 0 instead then it is an even numberCode:Answer = MyNumber Mod 2