Hit enter after type your search item

4 C++ Programs to Check Entered Number is EVEN or ODD

Check if a number is even or odd in C++ Program

In this short tutorial series of C++, we will show you how to check whether a number entered by the user is Even or Odd.

  • The number which can be divided by 2 is even
  • The number that can’t be divided by 2 is odd

First Program – Using % operator in if else

The % operator is used to return the remainder after division. As such, the remainder of a number that can be divided by two is always zero – we can use it to achieve our task i.e. a number input by the user is even or odd.

See the C++ program below that uses the % operator with if..else statements. For taking user input, we used C++ cin as shown in the program below:


Sample Output 1:

CPP-even-odd

Sample Output 2

CPP-even-odd-modulus

Second Approach – Using Bitwise operator AND

First, have a look at this C++ program that uses AND bitwise operator to check if the number is Even or Odd:


Sample output 1

CPP-even-odd-bitwise

Sample output 1

CPP-even-odd-bitwise-2

Third Approach – Using ternary operator example

This program also tells if the entered number is even or odd by using the ternary operator. Have a look at the code and sample outputs:


Output:

CPP-even-odd-ternary

Fourth Solution – Using Switch Case Statement

As such, the switch case is also a decision-making statement in C++. We include this example just to add another option if you get this assignment.

This example also uses the modulus operator %), just like with the if statement and remainder options are used as cases.

See the code and sample output below:


Output:

CPP-even-odd-switch

 

This div height required for enabling the sticky sidebar