Discuss Scratch

spaceinvader555
Scratcher
95 posts

How to find if a variable is even or odd?

I am making a complicated math project and I need a way for it to tell the difference between even and odd integers.
theanonymousweirdo
Scratcher
500+ posts

How to find if a variable is even or odd?

if <((Number) mod [2]) = [0]> then
even
else
odd

Last edited by theanonymousweirdo (Nov. 30, 2021 16:43:37)

EDGE_X
Scratcher
100+ posts

How to find if a variable is even or odd?

There are 2 ways I know. The first one is this:

if <((number::grey) mod (2)) = (0)> then
...
else
...

The other way is way more complicated and isn't necessary:

if <<(letter (length of (number::grey)) of (number::grey)) = (2)> or <<(letter (length of (number::grey)) of (number::grey)) = (4)> or <<(letter (length of (number::grey)) of (number::grey)) = (6)> or <<(letter (length of (number::grey)) of (number::grey)) = (8)> or <<(letter (length of (number::grey)) of (number::grey)) = (0)>>>>>> then
...
else
...

I recommend you use the first one as this other one is a waste of time.

Last edited by EDGE_X (Nov. 30, 2021 17:02:12)

spaceinvader555
Scratcher
95 posts

How to find if a variable is even or odd?

oh, I used a script that divided by 2, then rounded, them multiplied by 2, then checked if that was equal to the original. Yours is better. ¯\_(ツ)_/¯
Rcoder510
Scratcher
37 posts

How to find if a variable is even or odd?

The scripts before the “else” part runs if it is even, while the other runs if it is odd.
Here is the code:
if <((variable) / (2)) = (round ((variable) / (2)))> then 
. . .
else
. . .
end
helloworldbyeworld
Scratcher
1000+ posts

How to find if a variable is even or odd?

Rcoder510 wrote:

The scripts before the “else” part runs if it is even, while the other runs if it is odd.
Here is the code:
if <((variable) / (2)) = (round ((variable) / (2)))> then 
. . .
else
. . .
end
This one might also work, though I think the modulo method is the simplest.

Powered by DjangoBB