Does anybody have any good code on how to determine whether a number is even or odd? I came up with some simplistic code using a For Next loop, but im looking for something better. Thanx a lot.
Printable View
Does anybody have any good code on how to determine whether a number is even or odd? I came up with some simplistic code using a For Next loop, but im looking for something better. Thanx a lot.
Use Mod:
-------------
If Number Mod 2 = 0 then ' Number is even
Else ' Number is odd
End If
-------------
This gives you an idea of how to determine whether a number is even or odd.
Hope this helps.Code:Dim mlimit As Integer
Dim num As Integer
mlimit = 20
Case EVEN
For num = 0 To mlimit
If num Mod 2 = 0 Then
If num > 0 Then Debug.Print num;
End If
Next num
Case ODD
For num = 0 To mlimit
If num Mod 2 = 1 Then
If num > 0 Then Debug.Print num;
End If
Next num
Ruchi
[This message has been edited by Ruchi (edited 01-08-2000).]
You can also use this function which returns True if the number is even, False if it is odd.
Function IsEven(iEven as Long) as Boolean
IsEven = (iEven mod 2 = 0)
End Function