The simple Even and Odd program in C++ , i hope you will like this.
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.
#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.
#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
img credz: pixabay.com
Nice, you got a 2.0% @trafalgar upgoat, thanks to @wajahatsardar
Want a boost? Minnowbooster's got your back!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
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
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
great
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit