This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Vliekkie's profile picture

(QUESTION) even or odd numbers

Started by Vliekkie, 27 March 2012 - 02:07 PM
Vliekkie #1
Posted 27 March 2012 - 04: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/>/>
aferral #2
Posted 27 March 2012 - 04:12 PM
I was looking for the same thing look this link

http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=5559

im using bit.band (x, 1) if x is odd gives 1 and if x is even gives 0
Vliekkie #3
Posted 27 March 2012 - 04:34 PM
Ok.. i see it.. i feel it.. i memorized it..
BUT i dont get it..
Vliekkie #4
Posted 27 March 2012 - 04: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

?
kamnxt #5
Posted 27 March 2012 - 05:03 PM
You can use

print("Even or odd?")
i = tonumber(read())
if i%2 == 1 then
print("Odd.")
else
print("Even.")
end
Vliekkie #6
Posted 27 March 2012 - 05:07 PM
wont that just ONLY print odd if you inputed 2.. and even if you inputed anything else
kamnxt #7
Posted 27 March 2012 - 05:10 PM
No, if i is an odd number, it returns 1, if it is even it returns 0. It's a remainder operator.
Vliekkie #8
Posted 27 March 2012 - 05:22 PM
lol i tested it.. it works.. thx.. this is so much easier for me