Jump to content

test for odd/even number


Recommended Posts

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

Link to comment
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.
Link to comment
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...

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...