Sign in to follow this  
Followers 0
vickerps

odd or even

10 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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 this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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.

Edit2: Added EndFunc =-o

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 this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

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 this post


Link to post
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


AutoIt Links

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

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 this post


Link to post
Share on other sites

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

<{POST_SNAPBACK}>

Why not? :idiot:
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 this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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 this post


Link to post
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]

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