0
I need a code to find even and odd numbers using the isEven and isOdd functions
5 Antworten
+ 3
A number n is even if n modulo 2 = 0.
+ 3
You can either use modulo operator as suggested by Oma Falk and Sakshi , or for faster evaluation you can also make use of the fact that all even number have their least significant bit off in their binary representation, meaning bitwise AND of an even number and one should always be zero.
So the function might look something like this
```
bool is_even (const int number)
{
return ( !(number&1) )
}
```
+ 3
Arsenic ...which is the most elegant solution although advanced
+ 1
I suggest you firstly attempt the code, if your program will not run then ask the doubt from Q&A Discussions
+ 1
I hint you:
int n;
If(n%2==0)
{
cout<<"even";
}
else
{
cout<<"odd"
}
Now try to put in function