Odd/Even?

They have: 117 posts

Joined: Mar 2000

Just a quick question: In Perl, how would you determine if a number is an odd or even number?

Thanks!

--Edge

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

ehh.. hmm..

I've not used perl for a while, but try something like this:

$tmp = $number % 2;
if ($tmp == 1) {
  # ODD
}
else {
  # EVEN
}
'
the percent sign (in PHP anyway) is MOD or modulus. Modulus returns the remander of a division opperation. So the remainder of 2/2 is 0, but the remainder of 3/2 is 1.

Mark Hensler
If there is no answer on Google, then there is no question.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.