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:

  1. Input a number
  2. Check if its remainder is 0 on dividing with 2 (using %2)
  3. Using if-else find number is even or odd.

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.

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019