How do I determine if a number is even or odd?

An even number is any number that can be divided by 2 with no remainder. ColdFusion provides a function, mod, that returns the remainder of a division operation. To determine if a number is even, simply see if the value, modded by 2, returns 0:

<cfset x = 5>
<cfif x mod 2 is 0>
	The number is even.
<cfelse>
	The number is odd.
</cfif>

If the remainder is 1, then the number is odd.

This question was written by Raymond Camden
It was last updated on February 3, 2006.

Categories

Numbers

Comments

comments powered by Disqus