View Full Version : Simple Even / Odd Distinction
Candrias77
01-17-2003, 12:45 AM
I am wanting to have a line of code that determines if a number is even or odd. Something like:
<? if ($n == ODD) { echo RESULT1 } else { echo RESULT2 } ?>
brothercake
01-17-2003, 12:53 AM
er ... well I don't actually know how to get an integer in PHP .. but in javascript it would be
if ( parseInt(i/2) != (i/2) && i>0 ) { ... number is odd
else if (i==0) { ... it's 0
else { .. number is even
so if you can find out the function, the logic would be the same
Candrias77
01-17-2003, 01:07 AM
Anyone know how to put this into php?
I have been playing around with is_int() but no luck yet :(
Dylan Leblanc
01-17-2003, 01:29 AM
If your numbers are already integers:
if ((a % b) == 1) { // then odd }
else { // even }
Candrias77
01-17-2003, 01:41 AM
Thanks guys, ($n % 2 == 1) is perfect.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.