Program to check odd or even in C++
Written by
Any number that can be perfectly divisible by 2 is known as an even number. While the one which does not divides by 2 perfectly is known as an odd number.
Perfectly divisible means, that the number on division produces 0 as remainder.
Algorithm:
- Prompt the user to input a number.
- Use the modulo operator (
%
) to check if the remainder of the number divided by 2 is 0. - Use an
if
statement to determine if the number is even or odd. If the remainder is 0, the number is even. Otherwise, it is odd. - Print a message indicating whether the number is even or odd.
- Exit the program.
The below code checks if the integer entered by the user is even or odd:
#include <iostream>
using namespace std;
int main()
{
int num;
//Input the number
cout<<"Enter an integer: ";
cin>>num;
if (num % 2 == 0)//check its divisibility with 2
cout<< num << " is even.";
else
cout<< num <<" is odd.";
return 0;
}
Output:
Enter an Integer:234
234 is even.