Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Math are hard as well?

Asked by
gloveshun 119
5 years ago

How do i detect if a number is a multiple of 2?

0
i need help! gloveshun 119 — 5y

3 answers

Log in to vote
2
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

using the modulus operator What the following code says is 'if the remainder of a number divided by two is 0, then it is divisible by 2 otherwise there is a remainder of ____'

number = 16

if((number % 2)==0)then
        print("divisible")
else
        print("there is a remainder of ", number%2)
end
2
this time you beat me to it lol. and yours is much better than mine GGRBXLuaGG 417 — 5y
0
???????????? royaltoe 5144 — 5y
0
that was a pointing fingers emoji not sure why it defaulted to question marks. royaltoe 5144 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You would use "no % 2" so it would be something like this:

print(1%2 == 0) -- prints false because it's not even
print(2%2 == 0) -- prints true because it's even
print(3%2 == 0) -- false
print(4%2 == 0) -- true

I think that the % operator gives you the remaining of a multiplication or something but I am not sure

Log in to vote
0
Answered by 5 years ago

You can use the modulus operator(%), which finds out the remainder in division, such as

6รท4=1...2

is equal to

6%4=2

You can use it in this way:

local num = 8
if num%2 == 0 then -- Checks whether the remainder of 8/2 is equal to 0
-- Do something here
end

Answer this question