Results 1 to 3 of 3

Thread: telling odd and even numbers

  
  1. #1
    dracule's Avatar
    dracule is offline Senior Member -Hacks Veteran
    Join Date
    Oct 2005
    Posts
    1,447
    Rep Power
    17

    Default

    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


    Fd! will forever be my hero. Please take the time to vote for my group for Ecomagination: here Thanks for voting FD., Jibbz, OGTiago, Scorpus, Jsamp, killaj

  2. #2
    lordnikon is offline -Hacks Neophyte
    Join Date
    Jun 2006
    Posts
    76
    Rep Power
    14

    Default

    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
    i have a fat PSP.
    No, I will not post the damn firmware history.
    Hippy.

  3. #3
    romero126 is offline -Hacks Neophyte
    Join Date
    Jan 2006
    Posts
    81
    Rep Power
    14

    Default

    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

Posting Permissions

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