odd or even

Recommended Posts

How can i get autoit to know whether a number is odd or even?

Share on other sites

First of all try by reading the help file.. save these forums as the last resort (as someone else has already said...)

From the Mod() function's help reference:

```\$n = 18
If mod(\$n, 2) = 0 Then
MsgBox(0,"", \$n & " is an even number.")
Else
MsgBox(0, "", \$n & " is an odd number.")
EndIf

\$x = mod(4, 7);\$x == 4 because the divisor > dividend

\$y = mod(1, 3/4);\$y == 0.25 because the divisor is a float```
Edited by erebus

Share on other sites

I'm not exactly sure what Mod() does, but You could write it in your own function,

```Func OddEven(\$num)
If StringInStr(\$num, ".") = 0 Then
\$lastdig = StringTrimRight(\$num, 1)
If \$lastdig = 1 or \$lastdig = 3 or \$lastdig = 5 or \$lastdig = 7 or \$lastdig = 9 Then
Return 1
EndIf
Else
Return 0
EndIf
EndFunc```

I'm not sure what decimals are considerred lol but that should return 1 if the number is odd and 0 if the number is either even or containts a decimal =\ I didn't test but it should work

Edit:

Ah after looking at the helpfile, I know what mod does =\ That works too.

Edited by the_lord_mephy

My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Share on other sites

Your funcs a little messed up, you put endif instead of endfunc (or you just forgot the endfunc) to start

Also for me, it just dosnt work.

Try this and you'll see what i meen

```MsgBox (0, "", Oddeven("1"))

Func OddEven(\$num)
If StringInStr(\$num, ".") = 0 Then
\$lastdig = StringTrimRight(\$num, 1)
If \$lastdig = 1 or \$lastdig = 3 or \$lastdig = 5 or \$lastdig = 7 or \$lastdig = 9 Then
Return 1
Else
Return 0
EndIf
EndIf
EndFunc```
Edited by killaz219

Share on other sites

Since that wouldnt be totally accurate as a number with a . in it isnt even you should do it as follows...

```MsgBox (0, "", OddlyEven("1"))

Func OddlyEven(\$num)
If Not(StringInStr(\$num, ".")) Then
\$lastdig = StringTrimRight(\$num, 1)
If \$lastdig = 1 or \$lastdig = 3 or \$lastdig = 5 or \$lastdig = 7 or \$lastdig = 9 Then
Return 1
Else
Return 2
EndIf
ElseIf StringInStr(\$num, ".") Then
Return 0
EndIf
EndFunc```

Hope this helps some,

JS

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Share on other sites

JS, your script still returns the wrong number for odd values

Share on other sites

Why would anyone write an Odd/Even function unless it used mod? erebus is entrily correct.

@the_lord_mephy - The modulus (mod()) function returns the remainder of the division of the two parameters (only real numbers accepted). In erebus's example, if you divide the number by 2 and the remainder is 0 the number is even, if the number is greater than 0 then the number is odd. This can also be used to find multiples. (e.g. 6 is a multiple of 3 because mod(6, 3) = 0)

I hope this helps clear things up a bit.

*** Matt @ MPCS

Share on other sites

Why would anyone write an Odd/Even function unless it used mod?

<{POST_SNAPBACK}>

Why not?
```Func isEven(\$x)
If \$x/2 = Int(\$x/2) Then Return 1
EndFunc

;Example
For \$i = 0 to 10
If isEven(\$i) Then
MsgBox(4096,"", \$i & " is even")
Else
MsgBox(4096,"", \$i & " is odd")
EndIf
Next```

Mod would be my first choice, though.

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share on other sites

```Func wOddEven(\$wNum)
; -1 = ERROR, 1 = ODD, 2 = EVEN
If \$wNum = 0 OR StringInString(\$wNum,'.') <> 0 Then
Return -1
ElseIf StringInString(\$wNum / 2,'.') <> 0
Return 1
Else
Return 2
EndIf
EndFunc```

Edited by Wolvereness

Offering any help to anyone (to my capabilities of course)Want to say thanks? Click here! [quote name='Albert Einstein']Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.[/quote][quote name='Wolvereness' date='7:35PM Central, Jan 11, 2005']I'm NEVER wrong, I call it something else[/quote]

Share on other sites

Yeah matt I realized what it did after I posted my func, you must've not seen my edit =\

My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Create an account

Register a new account