Click to See Complete Forum and Search --> : Odd/even numbers
Steve1016
Jan 7th, 2000, 09:25 AM
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.
Juan Carlos Rey
Jan 7th, 2000, 09:42 AM
Use Mod:
-------------
If Number Mod 2 = 0 then ' Number is even
Else ' Number is odd
End If
-------------
Ruchi
Jan 7th, 2000, 12:50 PM
This gives you an idea of how to determine whether a number is even or odd.
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
Hope this helps.
Ruchi
[This message has been edited by Ruchi (edited 01-08-2000).]
Tonio169
Jan 9th, 2000, 04:05 PM
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.