Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate # How can I determine Even/Odd? [solved]

5 replies to this topic • Members
• 719 posts
• Last active: Nov 11 2014 07:12 PM
• Joined: 30 Aug 2012
Is there any way to determine if a number is even/odd without some absurd mathematical equation like this?

```InputBox, Input, AHK Numbers,,,160,100
Input := % Input / 2
Loop, Parse, Input,.
{
If A_Index = 2
{
Input := A_LoopField
Break
}
}
If Input = 0
MsgBox Even
Else
MsgBox Odd
Return```

I've looked around and didn't come up with anything. This seems like the sort of thing that'd already have been built into AHK. Does anyone have a less resource intensive or shorter solution to this issue?

What my script does here is divides your questionable number by 2, if the number is odd then it will have some value carried into its decimals and if it's even it won't. Then I grab the decimals using `Loop, Parse`, consequentially converting them to whole numbers but that's irrelevant, and compare them to 0. If the number is even then Input at this point will be 000000, if it's odd then Input will be something like 500000 or some other large number. Then by comparing these strings to 0 I can determine if the original Input was even or odd. • Members
• 790 posts
• Last active: Jan 05 2013 09:26 PM
• Joined: 28 Feb 2011
Two basic techniques:
• playing with bits: the last bit of an odd number must be 1 (not 0)
• modulus function
Results:
```Odd(n)
{
return n&1
}
Even(n)
{
return mod(n, 2) = 0
}
MsgBox % Odd(1) " - " Odd(2)
MsgBox % Even(1) " - " Even(2)```
You can of course choose one way (e.g. as in the `Even()` function above) and define the other function as the opposite (do `return !Even(n)` in the `Odd()` function).
Join the discussion on The future of AutoHotkey Visit me on github Win7 HP SP1 64bit | AHK_L U 64bit • Spam Officer
• 3757 posts
• • Last active: Apr 03 2016 08:47 PM
• Joined: 28 Feb 2012
In one function
```number = 8

msgbox % OddOrEven(number)

OddOrEven(num)

{

return ((Num & 1) != 0) ? "Odd" : "Even"

}```
Hope it helps • Members
• 719 posts
• Last active: Nov 11 2014 07:12 PM
• Joined: 30 Aug 2012
Thanks. I was reading something about bits that Skan posted during my search for a solution and couldn't figure out how to use it. Tyvm for the function Blockholyman. • Members
• 519 posts
• Last active: Jul 15 2017 12:04 PM
• Joined: 29 Jan 2009

as a alternative way you may use the build in function Mod() (edit just saw mod() in 2 posts up )

```Number:=12
If Mod(Number, 2)=0
MsgBox Even
Else MsgBox Odd
```
```Number:=13
If (Number & 1)  ; if last bit is 1
MsgBox Odd    ; then it's odd
Else MsgBox even ; else it's even
loop,15 {
msgbox,% "Index: " A_Index
. "`nNr_IsOdd: " Nr_IsOdd(A_Index)
. "`nNr_IsEven: " Nr_IsEven(A_Index)
. "`nNr_OddOrEven: " Nr_OddOrEven(A_Index)
}
Nr_OddOrEven(num){
; return, Mod(Num, 2)=0  ? "Even" : "Odd"
return (Num & 1 ? "Odd" : "Even")
}
Nr_IsOdd(num){
; return  Mod(Num, 2)=0  ? 0 : 1
return (Num & 1)
}
Nr_IsEven(num){
; return  Mod(Num, 2)=0  ? 1 : 0
return !(Num & 1)
}
```

Stopwatch emdkplayer
the code i post falls under the: WTFYW-WTFPL license

http://www.ahkscript.org/ the new forum • Moderators
• 237 posts
• Connoisseur of Boobs
• Last active: Jan 04 2016 11:12 PM
• Joined: 09 Oct 2012
To contradict (and hopefully allow for a bit more understanding about) the previous answers and code, the MOD() function (as well as using "&1") both require 2 checks.

An Output of 0 is Even, and 1 is Odd.

See the following examples:
```Number:=1

MsgBox,% "Mod(" Number ",2): " Mod(Number,2)
MsgBox,% Number "&1: " Number&1

Number:=2

MsgBox,% "Mod(" Number ",2): " Mod(Number,2)
MsgBox,% Number "&1: " Number&1

Number:="a"

MsgBox,% "Mod(" Number ",2): " Mod(Number,2)
MsgBox,% Number "&1: " Number&1```
Therefore: (THIS IS NOT TRUE)
```Number:="a"

If Number&1  ; if last bit is 1
MsgBox %Number% is Odd    ; then it's odd
Else MsgBox %Number% is Even ; else it's even```
You MUST check that the contents are NOT blank and then check Odd/Even Status.
```Number:="a"

If (Number&1 != ""){  ; if data qualifies as a number
If Number&1
MsgBox Odd    ; then it's odd
Else MsgBox Even ; else it's even
}else MsgBox, %Number% is not a number.```

Finally, a string that ENDS in a number is still NOT a number.

```Number:="abcd1234"

If (Number&1 != ""){  ; if data qualifies as a number
If Number&1
MsgBox Odd    ; then it's odd
Else MsgBox Even ; else it's even
}else MsgBox, %Number% is not a number.```