Learn To Code : Simple Even and Odd program in C++

in programiz •  7 years ago 

The simple Even and Odd program in C++ , i hope you will like this.

cpp_logo.png

Integers which are perfectly divisible by 2 are called even numbers.

And those integers which are not perfectly divisible by 2 are not known as odd number.

To check whether an integer is even or odd, the remainder is calculated when it is divided by 2 using modulus operator %. If remainder is zero, that integer is even if not that integer is odd.

odd.JPG

#include < iostream >
using namespace std;

int main( )

 {
int num;

cout << "Enter an integer":<<endl ;
cin >> num;

if ( num % 2 == 0)
    cout << num << " is even.";
else
    cout << num<< " is odd.";

return 0;

}

Enter an integer: 25
25 is odd.

Next time i enter :

Enter an integer:14
14 is even

In this program, if-else statement is used to check whether num%2 == 0 is true or not. If this expression is true, num is even if not num is odd.

Another Example for Even and Odd program.
you can also write program like this.

even odd.JPG

#include < iostream >
using namespace std;

int main()
{
int num;

cout << "Enter an integer: ";
cin >> num;

(num % 2 == 0) ? cout << num << " is even." : cout << num << " is odd.";

return 0;

}

Output :

Enter an integer: 27
27 is Odd

You can also use ternary operators ?: instead of if..else statement. Ternary operator is short hand notation of if-else statement.

Follow @wajahatsardar

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Calling @originalworks :)
img credz: pixabay.com
Nice, you got a 2.0% @trafalgar upgoat, thanks to @wajahatsardar
Want a boost? Minnowbooster's got your back!

The @OriginalWorks BETA V2 bot has upvoted(0.5%) and checked this post!
Some similarity seems to be present here:
https://www.cprogramming.com/tutorial/modulus.html
This is an early BETA version. If you cited this source, then ignore this message! Reply if you feel this is an error.

bro, there is some sort of error. i write and compile these program my self. it is a program of C++ so mostly program look similar to each other

great