Wednesday 11 September 2013

using mod function to find odd and even number

It is possible to simple use mod function to find odd or even number.

odd(x)     (mod(x,2) = = 1)
even(x)    (mod(x,2) = = 0)


How does modulo works? It is simply take the first number divide by the second number and get the remaining value.

example:
mod(23,4) = 3;

4 x 5 = 20

and the remaining is 3. Therefore the answer is 3.

So, we can do use 2 as the number to divide to, and the remaining will be either 1 or 0. The value of 1, would of course be odd number, and 0 is even.

Please note that this will only work is x is a whole number.


No comments:

Post a Comment