Even or odd

How to write a program in which a number is accepted from the user and displays if the number is even or odd ?
1
2
3
4
5
cin>>number;
if (number%2==1)
cout<<"The number you entered is an Odd number";
else
cout<<"The number you entered is an Even number";
What will happen when "number%2==1" is used?
the user-input number will be divided by 2 and if the remainder(that is %) is (that is ==)1,that shows it is an odd number...else its an even number. Hey friend try to grab a textbook
it's simple.
general notation for even numbers is (2n)
therefore, 2 can divide all even numbers completely.

LOGIC;

1
2
3
4
5
6
7
int n;
cout<<"\n Enter number:";
cin>>n;
if(n%2==0)
cout<<"\n the number is even";
else
cout<<"\n the number is odd";
Thanks Matri X and markode .
An easier way is to read the binary if you are looking for a super fast check.
Look into bitshifting!!
Topic archived. No new replies allowed.