swin4ort Posted July 17, 2007 Posted July 17, 2007 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
Siao Posted July 17, 2007 Posted July 17, 2007 (edited) See the example of Mod() in the helpfile. Edited July 17, 2007 by Siao "be smart, drink your wine"
GMK Posted July 17, 2007 Posted July 17, 2007 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
evilertoaster Posted July 17, 2007 Posted July 17, 2007 Used this one a while back- Func IsEven($numner) If ($numner/2)=Round($numner/2) Then Return True Else Return False EndIf EndFunc
martin Posted July 17, 2007 Posted July 17, 2007 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.
evilertoaster Posted July 17, 2007 Posted July 17, 2007 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...
Toady Posted July 17, 2007 Posted July 17, 2007 This is in the help file, look at Mod() www.itoady.com A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now