Detecting if integer is even or odd?

inevit4ble

Well-Known Member
Reaction score
38
Hi Every1,

This is an easy one, but for the life of me I can't remember how to do it

What method do I use in-order the check if a given integer is even or odd?

It would be used for some If Statements

Much appreciated Thanks!
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
Divide it by 2, multiply it by 2 again and check if its equal to its original value. Keep in mind to use integers only and not reals.
 

azareus

And you know it.
Reaction score
63
Divide it by 2, multiply it by 2 again and check if its equal to its original value. Keep in mind to use integers only and not reals.

And the way that works is the fact that integers are floored. Therefore:
3/2 = 1
1*2 = 2
2 != 3
3 is odd.

32/2 = 16
16*2 = 32
32 = 32
32 is even.
 

Parsival

Active Member
Reaction score
6
Trigger:
  • EvenOrOdd
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • a Less or equal 1.00
        • Then - Actions
          • If (a Equal to 0.00) then do (Game - Display to (All players) the text: Given number is even.) else do (Game - Display to (All players) the text: Given number is odd.)
          • Skip remaining actions
        • Else - Actions
      • Set a = (a - 2.00)
      • Trigger - Run EvenOrOdd <gen> (checking conditions)


That's my idea.
 

inevit4ble

Well-Known Member
Reaction score
38
oOo

I can see by your example what the out come would be but what does mod actually do?
 

vypur85

Hibernate
Reaction score
803
I believe it's also explained in the editor itself.

Mod gives the balance of the resulting division.

Example:
(5 mod 2) Equals to 1
(10 mod 10) Equals to 0
(20 mod 10) Equals to 0
(23 mod 20) Equals to 3
etc...

In your case, you just need to check whenever it is 0. It simply means the value can be divided to 2 and gives no balance.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top