+ 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.
11 Réponses
+ 4
if(num%2==0) cout << "even";
else cout << "odd";
+ 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
+ 3
Dorian, I thought A&1 was faster, and many many people are not aware of bitwise and operator :P
+ 2
there isn't any question...
+ 2
By the way, an even number has its modulo 2 equal to 0 and an odd one a modulo 2 equal to 1.
+ 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.
+ 2
GG for the bitwise! Do you have some benchmark about gain (& vs %)?
+ 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;
}
+ 1
bro you may use this code for checking odd even number enjoy coding
+ 1
bitwise ftw
0
no&1==0 is faster than no%2==0