Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Even Odd number 1

Status
Not open for further replies.

AdamVerellen

IS-IT--Management
Sep 20, 2002
39
CA
I can't seem to find a function to tell if a number is even or odd. Someone lend a hand

Adam
 
Use the mod operator to determine the remainer. If you divide by 2 and the remainder is zero then the number is even.

if <Value> mod 2 = 0 then
'even
else
'odd
end if Thanks and Good Luck!

zemp
 
Public Function fnIsEven(inp As Long) As Boolean
fnIsEven = Not (inp Mod 2)
End Function


Note: You will need to error check input etc!
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Try
Code:
result = intX MOD 2
If the result is zero, intX is even. There's only one other possibility (provided intX is an integer) and that's result is one, which would mean intX is odd.

HTH,
David
 
i dont know if theres a built in function but try this

Private Function IsEven(YourVal As Integer) As Boolean
If YourVal Mod 2 Then
IsEven = False
Else
IsEven = True
End If
End Function

If somethings hard to do, its not worth doing - Homer Simpson
 
doh!!! LOL If somethings hard to do, its not worth doing - Homer Simpson
 
Well that seems to be fairly conclusive!
[smile]
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top