Sign in to follow this  
Followers 0
swin4ort

test for odd/even number

7 posts in this topic

How do you find if a number is odd or even more elegantly than:

If StringInStr("13579",StringRight($num,1)) Then ;odd/even test - check last digit

MsgBox(0,"Odd",$num)

Else

MsgBox(0,"Even",$num)

EndIf

Thanks

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

See the example of Mod() in the helpfile.

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

From the help file:

#include <math.au3>
$I_Var = InputBox('Odd or Even', 'Enter a number:')
$I_Result = _MathCheckDiv($I_Var, 2)
If $I_Result = -1 Or @error = 1 Then
   MsgBox(0,'','You did not enter a valid number')
ElseIf $I_Result = 1 Then
   MsgBox(0,'','Number is odd')
ElseIf $I_Result = 2 Then
   MsgBox(0,'','Number is even')
Else
   MsgBox(0,'','Could not parse $I_Result')
EndIf

Share this post


Link to post
Share on other sites

Used this one a while back-

Func IsEven($numner)
    If ($numner/2)=Round($numner/2) Then 
        Return True
    Else
        Return False
    EndIf
EndFunc

How about

Func IsEven($number)

return Mod($number,2) = 0

endfunc


Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.

Share this post


Link to post
Share on other sites

See the example of Mod() in the helpfile.

How about

Func IsEven($number)

return Mod($number,2) = 0

endfunc

I know this works also. You can even use

func IsEven($i)
    Return ($i/2)=Round($i/2)
EndFunc
if you wanted fewer lines. It's just mentioning another way of doing it.

This was the method I used once when coding on systems that didn't have a mod() operator in the lanauge...hum how barbaric...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0