+ 1

# Odd & Even

Please friends write any example about this! I'm a student & it was my 2nd day today in my college, so I need your help to tell me.

16th Jul 2016, 10:57 PM
Zahid Khan
+ 4
if(num%2==0) cout << "even"; else cout << "odd";
17th Jul 2016, 5:00 AM
PratiCk JariwAla
+ 3
A%2 or A&1 A%2 means A modulo 2 so it is 0 when even and 1 when odd A&1 means A bitwise and 1 making it get only the binary bit that means 1, then 0 when even and 1 when odd
17th Jul 2016, 1:58 AM
_Geometry dash_ _Roh_ (AKA NovaRate0315)
+ 3
Dorian, I thought A&1 was faster, and many many people are not aware of bitwise and operator :P
17th Jul 2016, 2:00 AM
_Geometry dash_ _Roh_ (AKA NovaRate0315)
+ 2
there isn't any question...
16th Jul 2016, 11:07 PM
Dorian
+ 2
By the way, an even number has its modulo 2 equal to 0 and an odd one a modulo 2 equal to 1.
16th Jul 2016, 11:09 PM
Dorian
+ 2
Modulo(%) gives remainder so if we perform num%2 then it will give (0 or 1) only . so if we get 0 then num is even otherwise num is odd.
17th Jul 2016, 8:44 AM
Vishal Koshti
+ 2
GG for the bitwise! Do you have some benchmark about gain (& vs %)?
17th Jul 2016, 2:32 PM
Dorian
+ 2
#include <iostream> using namespace std; int main () { int no; cout <<"enter a number"<<endl; cin>>no; if (no%2==0) cout <<"number is even"<<endl; else cout <<"number is odd"<<endl; return 0; }
17th Jul 2016, 6:13 PM
Tushar Chetan
+ 1
bro you may use this code for checking odd even number enjoy coding
17th Jul 2016, 6:14 PM
Tushar Chetan
+ 1
bitwise ftw
17th Jul 2016, 11:06 PM
_Geometry dash_ _Roh_ (AKA NovaRate0315)
0
no&1==0 is faster than no%2==0
8th Jan 2017, 10:46 PM
Joseph Kristiano