DEV Community

Niyoj Oli
Niyoj Oli

Posted on • Updated on

Multiple ways to find whether a given number is even or odd.

Hello, developers. Recently, we have started C programming classes in our college. And, with my very first program to check whether a number is even or odd, I had an idea to solve this program in various ways so, I began to research and code.

What were we used to?

In my school or in most places where I saw a code to check the number is even or odd, the most common solution was using modulus operator (%).

if(number%2 == 0)
   printf("The number is even");
else 
   printf("The number is odd.");
Enter fullscreen mode Exit fullscreen mode

Its a very simple logic, if a number is perfectly divisible by 2 then it is even or else, it is definitely odd. And there is no errors associated with this program. But, for programmers thinking out of the box or coming with different unique solutions is what that differs from normal people. And, of course, these practices are what that trains your minds and make you more creative. Like we were said,

I have no special talent. I am only passionately curious. -Albert Einstein

What was I able to do?

So, with this curiosity, I came to these ideas, nth power of -1 is either 1 or -1. 1 if the power raised is even and, -1 if the power raised is odd. So, I converted this idea into a simple code,

if(pow(-1, number) == 1)
   printf("The number is even.");
else 
   printf("The number is odd.");
Enter fullscreen mode Exit fullscreen mode

That was something we, used in our math classes.

Next, if we loop a function to nth times, and in each consecutive two loops if we first increment any variable, and again in next loop we decrement the same variable. Then, the variable that has been incremented and decremented, does not changes from its initial value as +1-1=0, if we loop even times but if we loop odd times some changes in value are seen in the variable. In this way, we checked a number whether it is even or odd.

a = 0;
last_increment = 0;

for(i = number; i>=1; i--) {
   if (last_increment) {
      a--;
      last_increment = 0;
   } else {
      a++;
      last_increment = 1;
   }
}

if(a)
   printf("The number is odd.");
else 
   printf("The number is even.)
Enter fullscreen mode Exit fullscreen mode

Here, I also utilized the concept that zero means false and every non-zero number means true. So, I didn't have to write expressions like if(a!==0).

Similarly, my friend and I got this idea, where we loop a function 1 to number/2 times. And in every loop we subtract it by 2.So, for even number the value becomes 0. But for odd number, the value becomes either 1 or -1. So, we came with the code;

int i = number/2;
for(; i>=1; i--) {
   number -= 2;
}
if(number) 
   printf("The number is Even.");
else
   printf("The number is Odd.");
Enter fullscreen mode Exit fullscreen mode

Then, I stumbled upon some codes on Geeks For Geeks which also demonstrates the wise use of integer division in which you divide a number by 2 store it in a integer type variable and multiply it again by 2. For odd number, the number divide by 2 results a number with decimal values since, decimal part is not stored in integer type variable, when we again multiply it by 2, it's not the original number. But for even number since it is perfectly divisible by 2, when it is multiple by 2 again no changes occurs. So, the code can be;

int expression = number/2;
if (expression*2 == number)
   printf("The number is Even.");
else
   printf("The number is Odd.");
Enter fullscreen mode Exit fullscreen mode

That was a simple but clever idea. In that post, again there is use of Bit-wise operator and looping statement which might be interesting to know how it works. You can check it out through this link.

In this way we came to various approaches. You can look at that approaches in our GitHub Repo. This is a public repo where I tried to demonstrate maximum ways to check the number. You can look at those solutions, add yours if you have some new one. It can be a perfect way to start open source contribution too.

So, at the end what I came to conclusion was, solving a problem starts with analyzing and defining what the real problem is. Once you defined the problem, you can come to solution. Here, in this project, I started with what is really an even or an odd number. I defined it in multiple ways I could. So, at the end each unique definition gave me a unique solution.

Top comments (4)

Collapse
 
pauljlucas profile image
Paul J. Lucas

There's really no point to using anything other than % since all the other ways are less efficient. One alternative for integers that you omitted is n & 1: if it's 0, it's even; if it's 1, it's odd.

Collapse
 
niyoj profile image
Niyoj Oli

Thank you for your comment. Didn't mentioned that one in blog but I have mentioned it in my GitHub repo as it could make the blog very lengthy. Yeah, there is no point to using anything other than % if we want to be efficient but this blog is only trying to demonstrate there are many ways to solve a particular problem.

Collapse
 
pauljlucas profile image
Paul J. Lucas

Perhaps pick problems that actually have more than one reasonable solution to blog about? There certainly are many of those to choose from.

Thread Thread
 
niyoj profile image
Niyoj Oli

Thank you for your suggestion. This one is my first blog. I will consider that for next blogs.