This is my code to check if a number is even or odd
local number = 1
if number == 1 then
print("number is odd")
elseif number == 2 then
print("number is even")
elseif number == 3 then
print("number is odd")
elseif number == 4 then
print("number is even")
elseif number == 5 then
print("number is odd")
elseif number == 6 then
print("number is even")
elseif number == 7 then
print("number is odd")
elseif number == 8 then
print("number is even")
elseif number == 9 then
print("number is odd")
elseif number == 10 then
print("number is even")
elseif number == 11 then
print("number is odd")
elseif number == 12 then
print("number is even")
elseif number == 13 then
print("number is odd")
elseif number == 14 then
print("number is even")
elseif number == 15 then
print("number is odd")
elseif number == 16 then
print("number is even")
elseif number == 17 then
print("number is odd")
elseif number == 18 then
print("number is even")
elseif number == 19 then
print("number is odd")
elseif number == 20 then
print("number is even")
elseif number == 21 then
print("number is odd")
elseif number == 22 then
print("number is even")
elseif number == 23 then
print("number is odd")
elseif number == 24 then
print("number is even")
elseif number == 25 then
print("number is odd")
elseif number == 26 then
print("number is even")
elseif number == 27 then
print("number is odd")
elseif number == 28 then
print("number is even")
elseif number == 29 then
print("number is odd")
elseif number == 30 then
print("number is even")
elseif number == 31 then
print("number is odd")
elseif number == 32 then
print("number is even")
elseif number == 33 then
print("number is odd")
elseif number == 34 then
print("number is even")
elseif number == 35 then
print("number is odd")
elseif number == 36 then
print("number is even")
elseif number == 37 then
print("number is odd")
elseif number == 38 then
print("number is even")
elseif number == 39 then
print("number is odd")
elseif number == 40 then
print("number is even")
elseif number == 41 then
print("number is odd")
elseif number == 42 then
print("number is even")
elseif number == 43 then
print("number is odd")
elseif number == 44 then
print("number is even")
elseif number == 45 then
print("number is odd")
elseif number == 46 then
print("number is even")
elseif number == 47 then
print("number is odd")
elseif number == 48 then
print("number is even")
elseif number == 49 then
print("number is odd")
elseif number == 50 then
print("number is even")
elseif number == 51 then
print("number is odd")
elseif number == 52 then
print("number is even")
elseif number == 53 then
print("number is odd")
elseif number == 54 then
print("number is even")
elseif number == 55 then
print("number is odd")
elseif number == 56 then
print("number is even")
elseif number == 57 then
print("number is odd")
elseif number == 58 then
print("number is even")
elseif number == 59 then
print("number is odd")
elseif number == 60 then
print("number is even")
elseif number == 61 then
print("number is odd")
elseif number == 62 then
print("number is even")
elseif number == 63 then
print("number is odd")
elseif number == 64 then
print("number is even")
elseif number == 65 then
print("number is odd")
elseif number == 66 then
print("number is even")
elseif number == 67 then
print("number is odd")
elseif number == 68 then
print("number is even")
elseif number == 69 then
print("number is odd")
elseif number == 70 then
print("number is even")
elseif number == 71 then
print("number is odd")
elseif number == 72 then
print("number is even")
elseif number == 73 then
print("number is odd")
elseif number == 74 then
print("number is even")
elseif number == 75 then
print("number is odd")
elseif number == 76 then
print("number is even")
elseif number == 77 then
print("number is odd")
elseif number == 78 then
print("number is even")
elseif number == 79 then
print("number is odd")
elseif number == 80 then
print("number is even")
elseif number == 81 then
print("number is odd")
elseif number == 82 then
print("number is even")
elseif number == 83 then
print("number is odd")
elseif number == 84 then
print("number is even")
elseif number == 85 then
print("number is odd")
elseif number == 86 then
print("number is even")
elseif number == 87 then
print("number is odd")
elseif number == 88 then
print("number is even")
elseif number == 89 then
print("number is odd")
elseif number == 90 then
print("number is even")
elseif number == 91 then
print("number is odd")
elseif number == 92 then
print("number is even")
elseif number == 93 then
print("number is odd")
elseif number == 94 then
print("number is even")
elseif number == 95 then
print("number is odd")
elseif number == 96 then
print("number is even")
elseif number == 97 then
print("number is odd")
elseif number == 98 then
print("number is even")
elseif number == 99 then
print("number is odd")
elseif number == 100 then
print("number is even")
end
Yeah so to put it Simply, You use Modulus ( % ) to Check if its even or odd
Modulus is Like Division, except it returns the remainder
local x = 3 -- Our Number
if x % 2 == 0 then -- Checks if there is no remainder, in this case, returns 1 so odd
print("even") -- If Equation Returns 0
else
print("odd") -- If Equation Returns 1
end
Your solution is pretty inefficient, if you want to go further than a 100 numbers, here’s what I suggest you use.
This solution will let you check if numbers are even or odd until 1000 for example, however you can try any number as the argument.
local function checkIfNumbersAreEvenOrOdd(Until)
local SourceCode = "local number = 1\n"
local Even = false
for i = 1, Until do
if i == 1 then
SourceCode = SourceCode..string.format("if number == %s then",i).."\n"
else
SourceCode = SourceCode..string.format("elseif number == %s then",i).."\n"
end
if Even then
SourceCode = SourceCode..[[ print("number is even")]].."\n"
else
SourceCode = SourceCode..[[ print("number is odd")]].."\n"
end
Even = not Even
end
SourceCode = SourceCode.."end"
return SourceCode
end
print(checkIfNumbersAreEvenOrOdd(1000))
This is unnecessary, the Modulus here divides by 2, if you have 1000, it will return 0, normal division would be 500, if you have 1001, it will return 1, which is odd
I tested the code, i know it works
If anything, this code is unnecessary for a simple equation / number check
Um…no. To actually check, do a division modulo 2 operation.
local function checkEven(x)
if x % 2 == 0 then
return true
end
return false
end
If you are using 32-bit integers, an alternative way is this:
local function bit32even(x)
if bit32.band(x, 0x00000001) == 0 then
return true
end
return false
end
The way that this works is that it masks off all but the least significant bit (the 1’s position). If that bit is set (1), then the number is odd. If that bit is not set (0), then it’s even.
That second method only works on integers. For me coming for a C/Assembler background, it’s a simple AND instruction on the CPU. Besides, coding at that level, everything is integer. You learn how to do bit manipulation really quick.
if number == 1 then
print("number is odd")
elseif number == 2 then
print("number is even")
elseif number == 3 then
print("number is odd")
elseif number == 4 then
print("number is even")
elseif number == 5 then
print("number is odd")
elseif number == 6 then
print("number is even")
elseif number == 7 then
print("number is odd")
elseif number == 8 then
print("number is even")
elseif number == 9 then
print("number is odd")
elseif number == 10 then
print("number is even")
elseif number == 11 then
print("number is odd")
elseif number == 12 then
print("number is even")
elseif number == 13 then
print("number is odd")
elseif number == 14 then
print("number is even")
elseif number == 15 then
print("number is odd")
elseif number == 16 then
print("number is even")
elseif number == 17 then
print("number is odd")
elseif number == 18 then
print("number is even")
elseif number == 19 then
print("number is odd")
elseif number == 20 then
print("number is even")
elseif number == 21 then
print("number is odd")
elseif number == 22 then
print("number is even")
elseif number == 23 then
print("number is odd")
elseif number == 24 then
print("number is even")
elseif number == 25 then
print("number is odd")
elseif number == 26 then
print("number is even")
elseif number == 27 then
print("number is odd")
elseif number == 28 then
print("number is even")
elseif number == 29 then
print("number is odd")
elseif number == 30 then
print("number is even")
elseif number == 31 then
print("number is odd")
elseif number == 32 then
print("number is even")
elseif number == 33 then
print("number is odd")
elseif number == 34 then
print("number is even")
elseif number == 35 then
print("number is odd")
elseif number == 36 then
print("number is even")
elseif number == 37 then
print("number is odd")
elseif number == 38 then
print("number is even")
elseif number == 39 then
print("number is odd")
elseif number == 40 then
print("number is even")
elseif number == 41 then
print("number is odd")
elseif number == 42 then
print("number is even")
elseif number == 43 then
print("number is odd")
elseif number == 44 then
print("number is even")
elseif number == 45 then
print("number is odd")
elseif number == 46 then
print("number is even")
elseif number == 47 then
print("number is odd")
elseif number == 48 then
print("number is even")
elseif number == 49 then
print("number is odd")
elseif number == 50 then
print("number is even")
elseif number == 51 then
print("number is odd")
elseif number == 52 then
print("number is even")
elseif number == 53 then
print("number is odd")
elseif number == 54 then
print("number is even")
elseif number == 55 then
print("number is odd")
elseif number == 56 then
print("number is even")
elseif number == 57 then
print("number is odd")
elseif number == 58 then
print("number is even")
elseif number == 59 then
print("number is odd")
elseif number == 60 then
print("number is even")
elseif number == 61 then
print("number is odd")
elseif number == 62 then
print("number is even")
elseif number == 63 then
print("number is odd")
elseif number == 64 then
print("number is even")
elseif number == 65 then
print("number is odd")
elseif number == 66 then
print("number is even")
elseif number == 67 then
print("number is odd")
elseif number == 68 then
print("number is even")
elseif number == 69 then
print("number is odd")
elseif number == 70 then
print("number is even")
elseif number == 71 then
print("number is odd")
elseif number == 72 then
print("number is even")
elseif number == 73 then
print("number is odd")
elseif number == 74 then
print("number is even")
elseif number == 75 then
print("number is odd")
elseif number == 76 then
print("number is even")
elseif number == 77 then
print("number is odd")
elseif number == 78 then
print("number is even")
elseif number == 79 then
print("number is odd")
elseif number == 80 then
print("number is even")
elseif number == 81 then
print("number is odd")
elseif number == 82 then
print("number is even")
elseif number == 83 then
print("number is odd")
elseif number == 84 then
print("number is even")
elseif number == 85 then
print("number is odd")
elseif number == 86 then
print("number is even")
elseif number == 87 then
print("number is odd")
elseif number == 88 then
print("number is even")
elseif number == 89 then
print("number is odd")
elseif number == 90 then
print("number is even")
elseif number == 91 then
print("number is odd")
elseif number == 92 then
print("number is even")
elseif number == 93 then
print("number is odd")
elseif number == 94 then
print("number is even")
elseif number == 95 then
print("number is odd")
elseif number == 96 then
print("number is even")
elseif number == 97 then
print("number is odd")
elseif number == 98 then
print("number is even")
elseif number == 99 then
print("number is odd")
elseif number == 100 then
print("number is even")
elseif number == 101 then
print("number is odd")
elseif number == 102 then
print("number is even")
elseif number == 103 then
print("number is odd")
elseif number == 104 then
print("number is even")
elseif number == 105 then
print("number is odd")
elseif number == 106 then
print("number is even")
elseif number == 107 then
print("number is odd")
elseif number == 108 then
print("number is even")
elseif number == 109 then
print("number is odd")
elseif number == 110 then
print("number is even")
elseif number == 111 then
print("number is odd")
elseif number == 112 then
print("number is even")
elseif number == 113 then
print("number is odd")
elseif number == 114 then
print("number is even")
elseif number == 115 then
print("number is odd")
elseif number == 116 then
print("number is even")
elseif number == 117 then
print("number is odd")
elseif number == 118 then
print("number is even")
elseif number == 119 then
print("number is odd")
elseif number == 120 then
print("number is even")
elseif number == 121 then
print("number is odd")
elseif number == 122 then
print("number is even")
elseif number == 123 then
print("number is odd")
elseif number == 124 then
print("number is even")
elseif number == 125 then
print("number is odd")
elseif number == 126 then
print("number is even")
elseif number == 127 then
print("number is odd")
elseif number == 128 then
print("number is even")
elseif number == 129 then
print("number is odd")
elseif number == 130 then
print("number is even")
elseif number == 131 then
print("number is odd")
elseif number == 132 then
print("number is even")
elseif number == 133 then
print("number is odd")
elseif number == 134 then
print("number is even")
elseif number == 135 then
print("number is odd")
elseif number == 136 then
print("number is even")
elseif number == 137 then
print("number is odd")
elseif number == 138 then
print("number is even")
elseif number == 139 then
print("number is odd")
elseif number == 140 then
print("number is even")
elseif number == 141 then
print("number is odd")
elseif number == 142 then
print("number is even")
elseif number == 143 then
print("number is odd")
elseif number == 144 then
print("number is even")
elseif number == 145 then
print("number is odd")
elseif number == 146 then
print("number is even")
elseif number == 147 then
print("number is odd")
elseif number == 148 then
print("number is even")
elseif number == 149 then
print("number is odd")
elseif number == 150 then
print("number is even")
elseif number == 151 then
print("number is odd")
elseif number == 152 then
print("number is even")
elseif number == 153 then
print("number is odd")
elseif number == 154 then
print("number is even")
elseif number == 155 then
print("number is odd")
elseif number == 156 then
print("number is even")
elseif number == 157 then
print("number is odd")
elseif number == 158 then
print("number is even")
elseif number == 159 then
print("number is odd")
elseif number == 160 then
print("number is even")
elseif number == 161 then
print("number is odd")
elseif number == 162 then
print("number is even")
elseif number == 163 then
print("number is odd")
elseif number == 164 then
print("number is even")
elseif number == 165 then
print("number is odd")
elseif number == 166 then
print("number is even")
elseif number == 167 then
print("number is odd")
elseif number == 168 then
print("number is even")
elseif number == 169 then
print("number is odd")
elseif number == 170 then
print("number is even")
elseif number == 171 then
print("number is odd")
elseif number == 172 then
print("number is even")
elseif number == 173 then
print("number is odd")
elseif number == 174 then
print("number is even")
elseif number == 175 then
print("number is odd")
elseif number == 176 then
print("number is even")
elseif number == 177 then
print("number is odd")
elseif number == 178 then
print("number is even")
elseif number == 179 then
print("number is odd")
elseif number == 180 then
print("number is even")
elseif number == 181 then
print("number is odd")
elseif number == 182 then
print("number is even")
elseif number == 183 then
print("number is odd")
elseif number == 184 then
print("number is even")
elseif number == 185 then
print("number is odd")
elseif number == 186 then
print("number is even")
elseif number == 187 then
print("number is odd")
elseif number == 188 then
print("number is even")
elseif number == 189 then
print("number is odd")
elseif number == 190 then
print("number is even")
elseif number == 191 then
print("number is odd")
elseif number == 192 then
print("number is even")
elseif number == 193 then
print("number is odd")
elseif number == 194 then
print("number is even")
elseif number == 195 then
print("number is odd")
elseif number == 196 then
print("number is even")
elseif number == 197 then
print("number is odd")
elseif number == 198 then
print("number is even")
elseif number == 199 then
print("number is odd")
elseif number == 200 then
print("number is even")
but that takes too long to type , so i occasionally use
local num = 10
if num%2 == 0 then
print("it's even!")
else
print("it's odd >:(")
I really like the first solution, I don’t know why people are trashing on my original solution as well, it does the same thing, but automates it… I thought it was pretty clever… The modulo might be too expensive to calculate
@black1shadow1048, I’m sorry to copy your script here but, I’ve made some changes and it works:
local Number = 0
while task.wait(1) do
Number=math.random(0,999)
if Number%2 == 0 then
print(Number..", it's even")
else
print(Number..", it's odd")
end
end