# Thread: telling odd and even numbers

 Tweet
1. ## is there sometype of code that i can use to tell the difference between odd and even numbers? because i need to do this:
if variable is odd then
do this
end  Reply With Quote

2. -Hacks Neophyte Join Date
Jun 2006
Posts
76
Rep Power
14

## use the modulus operator, %

it acts like division but the answer is the remainder for example:

4 % 2 = 0 because 4 divided by 2 = 2 and there is no remained, however

25 % 4 = 1 because 25 can be divided by 4 5 times having a remained of 1.

So you could basically tell the difference between odds and evens by using modulus like this:

if (x % 2 > 0)
-- the number is odd

if (x % 2 == 0)
-- the number is even

:)
hope that helps but i dunno if lua supports modulus :\ cuz C++ and java do  Reply With Quote

3. -Hacks Neophyte Join Date
Jan 2006
Posts
81
Rep Power
14

## Happy Birthday.
Code:
```function math:even(param)
local num = param/2
if (num == math.floor(num) ) then
return 1
end
return nil
end

function math:odd(param)
local num = param/2
if (not num == math.floor(num) ) then
return 1
end
return nil
end```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•