Jump to content




(QUESTION) even or odd numbers


7 replies to this topic

#1 Vliekkie

  • New Members
  • 17 posts
  • Location6 feet under.. the void ^^

Posted 27 March 2012 - 03:07 PM

I'm not sure how I want to say this but..

Is there a way to make a If statement that will run one command when a variable is a odd and another command when the variable is a even

lets say ..

y=5
thenumber=tonumber(y)

if thenumber="odd" then

print("durp")

else if thenumber="even" then

print("not durp")
end


Plz pay no atention to spelling and the failure code up there..

I just want to know what I would use instead of "odd" and "even"

PS My english might be suckish...
Also.. how to I set something to be a spoiler.. i dont want anyone to read through the code if they already know what i mean..

hope there is something like this :o/>

#2 aferral

  • New Members
  • 10 posts

Posted 27 March 2012 - 03:12 PM

I was looking for the same thing look this link

http://www.gammon.co...subject_id=5559

im using bit.band (x, 1) if x is odd gives 1 and if x is even gives 0

#3 Vliekkie

  • New Members
  • 17 posts
  • Location6 feet under.. the void ^^

Posted 27 March 2012 - 03:34 PM

Ok.. i see it.. i feel it.. i memorized it..
BUT i dont get it..

#4 Vliekkie

  • New Members
  • 17 posts
  • Location6 feet under.. the void ^^

Posted 27 March 2012 - 03:38 PM

Oh wait would i use this as

y=read() --your number
thenumber=bit.band(y,1)

if thenumber== ("1") then
print ("odd")

else if thenumber==("0") then
print("even")

end

?

#5 kamnxt

  • New Members
  • 80 posts

Posted 27 March 2012 - 04:03 PM

You can use
print("Even or odd?")
i = tonumber(read())
if i%2 == 1 then
print("Odd.")
else
print("Even.")
end


#6 Vliekkie

  • New Members
  • 17 posts
  • Location6 feet under.. the void ^^

Posted 27 March 2012 - 04:07 PM

wont that just ONLY print odd if you inputed 2.. and even if you inputed anything else

#7 kamnxt

  • New Members
  • 80 posts

Posted 27 March 2012 - 04:10 PM

No, if i is an odd number, it returns 1, if it is even it returns 0. It's a remainder operator.

#8 Vliekkie

  • New Members
  • 17 posts
  • Location6 feet under.. the void ^^

Posted 27 March 2012 - 04:22 PM

lol i tested it.. it works.. thx.. this is so much easier for me





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users