I need a code to find even and odd numbers using the isEven and isOdd functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I need a code to find even and odd numbers using the isEven and isOdd functions

15th Nov 2022, 6:13 AM
Nigel Andati
Nigel Andati - avatar
5 Answers
+ 3
A number n is even if n modulo 2 = 0.
15th Nov 2022, 6:15 AM
Oma Falk
Oma Falk - avatar
+ 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) ) } ```
19th Nov 2022, 3:12 AM
Arsenic
Arsenic - avatar
+ 3
Arsenic ...which is the most elegant solution although advanced
19th Nov 2022, 8:19 AM
Oma Falk
Oma Falk - avatar
+ 1
I suggest you firstly attempt the code, if your program will not run then ask the doubt from Q&A Discussions
15th Nov 2022, 7:08 AM
Sakshi
Sakshi - avatar
+ 1
I hint you: int n; If(n%2==0) { cout<<"even"; } else { cout<<"odd" } Now try to put in function
15th Nov 2022, 7:09 AM
Sakshi
Sakshi - avatar