How to check if a viable is even or odd

Oldest question in the history of humanity…

If the number is even, the remainder after the division by 2 is zero, and 1 if the number is odd, so using rem function (which actually calculates the remainder of the division between two numbers) in matlab we can check if remainder is zero or not.

Example: If my var is MyVar,

  rem(MyVar,2)=0 if MyVar is even

  rem(MyVar,2)=1 if MyVar is odd

If I want to do something if my variable is even (or odd), I can write:

   if rem(MyVar,2)


   if ∼rem(MyVar,2)

This entry was posted in checking format. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s