C++ Program to Check the Number is Odd or Even

What are odd and even numbers?

  1. Odd Numbers: The whole numbers that cannot be divided exactly into pairs or when divided by two, leave a reminder of 1 is called odd numbers
    •  Example : 1, 3, 5, 7, 9, 11, 13 15……
  2. Even Numbers: The whole numbers that can be divided exactly into pairs or when divided by 2, leave a remainder of 0 is called even numbers
    • Example: 2, 4, 6, 8, 10, 12, 14, 16,…….

How can we check whether the given number is odd or even?

For checking ether the given number is odd or even we commonly use the method of dividing the given number by 2. If the given number is evenly divisible by 2 with a reminder of 0 then the number is even. And if the number leaves a remainder of 1 then the given number is odd. For this, we can use the modulo operator  %.

The modulo operator, denoted by %, is an arithmetic operator which produces the remainder of an integer division.

We can calculate the reminder with the modulo operator % like this num % 2 = = 0

  • If num % 2 = = 0 Then it is an even number 
  • If num % 2 = = 1 Then it is an odd number

How can we check whether the given number is odd or even in C++?

Call the main function to start the program execution. Within the main body declare an integer type variable n. Ask the user to enter a number. Read this number to the variable n by using the object cin. Now check whether the remainder of n divided by 2 is 1 or 0. To find the reminder the arithmetic operator % is used. This can be used like n %  2 = = answer.

Now you have to check if it is 0 or 1. For this simple, if…..else statement is used.

In C++ the if….else statement is used to run one block of code under certain conditions and another block of code under different conditions.


if (condition)
{
……….
} else
{
……….
} 
 

Here we check whether (n % 2 = =0 ) is true or not. If the expression is true, n is even. Display a message the number is even.Else n is odd. Display a message the number is odd.

Let's take some simple examples to understand more about this.

  1. If the entered number is odd:

    Let the entered value be 'n = 7'. According to the logic, we used in the C program, if '(n%2 = 0)', then a is an Even number otherwise Odd. Here 'n = 7', then we have (7%2 not equal to zero), so the given number is Odd

  2. If the entered number is even:

    Let entered value 'n = 10'. In accordance with the program logic, if (n%2 == 0), then a is an Even number otherwise Odd. Here 'n = 10',then we have (10%2 = 0),so the given number is Even.

check whether a number is odd or even

Algorithm.

Step 1:  Call the header file iostream.

Step 2: Use the namespace std.

Step 3: Open the integer type main function; int main().

Step 4: Declare an integer type variable n.

Step 5: Print a message “Enter an integer”.

Step 6: Read the value to the variable n.

Step 7: Check the reminder ,if ( n% 2 = = 0) then Print n is even.else ,print n is odd.
Step 8 : Exit.
 

C++ Source Code

                                          #include <iostream>
using namespace std;

int main() {
  int n;

  cout << "Enter a Number ";
  cin >> n;

  if ( n % 2 == 0)
    cout << n << " is even.";
  else
    cout << n << " is odd.";

  return 0;
}
                                      

OUTPUT

Run 1
---------------------
Enter a Number: 10
10 is even.

Run2
---------------------
Enter a Number: 14
14 is even.