Check whether the input number is Even or Odd
An integer number which is evenly divisible by 2 is called even number. When I say evenly divisible, it means that if we divide the even number by 2, it yields no remainder. For example, 2, 4, 6, 8 …are even numbers.
An integer number which yields a remainder when divided by 2 is known as odd number. For example, 3, 5, 7, 9 are odd numbers.
Lets write a program to check even odd numbers
Example: Program to check whether the entered number is even or odd
#include <iostream>
using namespace std;
bool checkEvenOdd(int num);
int main(){
int num;
bool isEven;
cout<<"Enter any number: ";
//Storing the entered value in variable num
cin>>num;
//Calling the function that checks even odd
isEven = checkEvenOdd(num);
if(isEven)
cout<<num<<" is an even number";
else
cout<<num<<" is an odd number";
return 0; }
/* This function checks whether the passed number is even
* or odd. If the number is even then this function returns
* true else it returns false. */
bool checkEvenOdd(int num){
bool b;
/* If number is perfectly divisible by 2 then it is
* an even number else it is an odd number
* */
if (num % 2 == 0)
b=true;
else
b=false;
return b; }
Output
Enter any number: 101
101 is an odd number
Leave Comment