How do i detect if a number is a multiple of 2?
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
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
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