How to check if a number is ODD or Even ? [PHP function]


Hey guys!

Long time no seen! Today I had a pleasure of going over myprogrammingblog emails, where many users have asked me to show solutions to different problems. Sorry for not being able to respond earlier, as I had whole bunch of things going on.

From now on, at least for a month, I will be regularly (read as 1 time in 1-2 days) posting some code examples for the problems you have asked me to take a look at. By the way, if you have any interesting problems, which you would like to share with me, please email me at: myprogrammingblog@gmail.com .

Quick Update:

I have opened myprogrammingblog Git repo, where I will be posting code examples discussed in this blog.

Repo URL is :

https://github.com/myprogrammingblog/myprogrammingblog.com/

Now, back to business.

One of the readers ( I am not posting names/nicknames since some people don’t like to be addressed publicly) asked me:

How to check if a number is ODD or Even ?

To solve this problem I will use nice operator, available in most popular programming languages (e.g C/C++/Python/Java/PHP/JavaScript…you name it…) called modulo (%) .

Modulo – finds the remainder of the division.  That’s exactly what we need!

We know that EVEN numbers are those that can be equally (read without a remainder) divided into two groups. By using modulo we can check if the number divided by two does not have a remainder, than it is an EVEN number, otherwise it is ODD.

Bored already ? Here is the code (since no specific language was required, I have used PHP):

<?php

/*
 * Description:
*		This small function returns true if passed number
*                     is divisible by two, false if not.
*
* @author: Anatoly Spektor
*/
function isEven($number) {
	$isEven = false;
	if (is_numeric ($number)) {
		if ( $number % 2 == 0) $isEven = true;
	}
	return $isEven;
}

?>

In case you need Unit Tests for this function, they are available in the repo.

Thank you for sending me your questions!

Regards,

Anatoly

About these ads

About Anatoly Spektor

My name is Anatoly Spektor (originally Anatolijs Spektors) I am Software and Web Developer. I have worked in Seneca Center for Development of Open Technology on Big Blue Button Add-on - Polling Module. Currently, I am employed as Software Engineer in Red Hat.
This entry was posted in GitHub, open-source, PHP, Programming Languages, Useful Functions and tagged , , , , , , , , . Bookmark the permalink.

5 Responses to How to check if a number is ODD or Even ? [PHP function]

  1. Hello,
    Nice article. Thank you for sharing. I also shared a similar article on c programming. I posted a article to check whether a given number is odd or even using c program.

  2. John says:

    Very effective and commonsensical solution, I remember when I first learned programming, this was one of the first problems given to us, at that time it appeared tough.

  3. Pingback: How to check if a number is ODD or Even ? [PHP function] | Ragnarok Connection

  4. Pingback: The way to verify if a quantity is ODD or Even ? [PHP function] | Ragnarok Connection

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s