Real'sHowTo AddThis Feed Button
Custom Search

Detect even/odd numberTag(s): String/Number


Use the modulus operator.

For Other interesting modulus operations, see http://mindprod.com/jgloss/modulus.html

if (x % 2 == 0) {
  // even 
}

if (x % 2 != 0) {
  // odd
}

... or binary AND operator...

if (( x & 1 ) == 0) {
  // even
}

if (( x & 1 ) != 0) {
  // odd
}


blog comments powered by Disqus


If you find this article useful, consider making a small donation
to show your support for this Web site and its content.

Written and compiled by Réal Gagnon ©1998-2014
[ home ]