Advertisement

c++ - How to read a even or odd from a number?

Started by March 08, 2003 06:04 PM
11 comments, last by Delsaber 21 years, 8 months ago
This is in c++ How would I see if a certain number is odd or even? Would it be something like this (btw this doesnt work for me)?

#include <iostream.h>

int main()
{
int number;
double newnumber;
cout<<"Enter a number";
cin>>number;
newnumber=(double((number*(1/2))%2)); /*a even number should have a remainder of 0 after division*/
if(newnumber==0)
{
cout<<"The number is even";
}
else
{
cout<<"The number is odd";
}
return 0;
}
 
Now the problem is that even if I put in a odd number like 55, the program says ''even'', not odd like it should.

        inline bool IsOdd(int num){   return (num & 1);}inline bool IsEven(int num){   return !(IsOdd(num));}      


I think that works

edit:
yup, it works.

Now to explain it... every number is stored in binary, of course. Lets look at a table of all the numbers 0 through 10, in binary.

        0000 0000   (0)0000 0001   (1)0000 0010   (2)0000 0011   (3)0000 0100   (4)0000 0101   (5)0000 0110   (6)0000 0111   (7)0000 1000   (8)0000 1001   (9)0000 1010   (10)      


Notice something about the last bit in each of those numbers? The last bit on all even numbers (0, 2, 4, 6, 8, 10) is a 0. The last bit on all odd numbers (1, 3, 5, 7, 9) is 1.

So it can be theorized that if the least-significant bit in all integers is a 0, the number is even. If it is 1, it is odd. Therefor, to see if a number is even, we just need to look at the last bit.

How do we do that? We use the C/C++ bitwise and operator (&). This operator compares all the bits of two given values, and returns a third. For each bit in the original two numbers, the corresponding bit in the third number is a 1 if and ONLY IF the two original bits are both 1. A few examples:

5 & 7:

0000 0101 &
0000 0110
---------
0000 0100 = 4

36 & 2:
0010 0100 &
0000 0010
---------
0000 0000 = 0


Now, take the number 1, in binary.

0000 0001

The ONLY binary digit that has a 1 in the binary number 1 is the last one, or the least significant. So, if we & a number with 1, and that last digit in the answer comes out to be 1, then the number is odd!

example:

13 & 1:

0000 1101 &
0000 0001
---------
0000 0001

We got a result with the last digit being 1! So the number 13 is odd. Lets try an even number:

12 & 1:
0000 1100 &
0000 0001
---------
0000 0000

We got 0, or false! So the number 12 is Even!

[edited by - Ronin Magus on March 8, 2003 7:26:36 PM]

[edited by - Ronin Magus on March 8, 2003 7:36:31 PM]

[edited by - Ronin Magus on March 8, 2003 7:58:26 PM]
Advertisement
this way seems to work for me.
As far as i know, every even number is divisible by two.
if(number%2==0)
... number is even
else
... odd

[edited by - ph4ntasyc0d3 on March 8, 2003 7:13:34 PM]
As you said, an even number has a remainder of 0 when divided by 2. Hence,
if(n%2 == 0) /* it''s even */else /* it''s odd */ 
I tried the following and it worked as well - pick your solution

#include <stdio.h>

int main(int argc, char *argv[])
{
int a = 2;
int b = 55;

if (a%2 == 0)
printf("even\n");
else
printf("odd\n");

if (b%2 == 0)
printf("even\n");
else
printf("odd\n");

return 0;
}

For a I got even and for b I got odd.
The guy who invented the first wheel was an idiot. The guy who invented the other three, he was the genius.
I think you over complicated the problem a bit. After a minute, this is what I came up with:

#include <iostream>int main(){	long number;	std::cout << "Enter a number:\t";	std::cin >> number;	if(number%2 == 0)	{		std::cout << "It''s even\n";	}	else	{		std::cout << "It''s odd\n";	}	return 0;} 


It just takes the number you inputted and finds the remainder from dividing by 2. Since even numbers don''t have a remainder if you divide by 2, it says it''s even. Otherwise, if there is a remainder, it''s odd.
Advertisement
Oh I got it to work, thanks. I think I did overcomplicate the problem. Thanks guys
Oh I got it to work, thanks. I think I did overcomplicate the problem. Thanks guys
All those solutions work.. but remember that a bitwise & doesn''t require a divide, which can be much faster than %.
Wow, I like that little binary "trick". That was well written and easy to understand, even for me. I was going to suggest using the modulus operator as others had said, but the binary comparison is very interesting idea. I''ll have to try that too.

Peon
Peon

This topic is closed to new replies.

Advertisement