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

Words
118
Reading
1 min
Listen
Play
9y

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@wajahatsardar

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