Detect if number is odd or even.
#1

Hello!

How can I tell if a number is odd, or even?
Reply
#2

put them in a array? or make calculation system.
Reply
#3

from pawn-lang
Quote:

e1 % e2
Results in the remainder of the division of e1 by e2. The sign
of the remainder follows the sign of e2. Integer division and
remainder have the Euclidean property: D = q*d + r, where
q = D/d and r = D%d.

You would have to divide the number by 2 and if there is no remainder...its even. If there is a remainder...its odd.

I >>sUCK<< at math tho..so doulbe check that.

Code:
IsNumberEven(number) {
  new remainder = number % 2;
  if(remainder > 0) return false;
  else return true;
}
^try that, maybe?
Reply
#4

Test:
pawn Code:
#define IsOdd(%0) %0 % 2
then, !IsOdd = IsEven
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)