Open In App

C++ Program To Check Whether Number is Even Or Odd

A number is even if it is completely divisible by 2 without leaving a remainder. A number is odd if it is not completely divisible by 2 and leaves the remainder 1. In this article, we will learn to write a C++ program to check whether a number is even or odd.

Algorithm to Check Even or Odd Numbers

The idea is to use the Modulo operator to find the remainder after dividing by 2.



  • If the remainder is 0, print “Even”.
  • If the remainder is 1, print “Odd”.

For example, 2, 4, 6, 8, … are even numbers, and 5, 7, 9, 11, … are odd numbers.

C++ Program To Check Whether Number is Even Or Odd




// C++ program to check
// for even or odd
#include <iostream>
using namespace std;
  
// Returns true if n is
// even, else odd
bool isEven(int n) { return (n % 2 == 0); }
  
// Driver code
int main()
{
    int n = 101;
    if (isEven(n))
        cout << "Even";
    else
        cout << "Odd";
  
    return 0;
}

Output

Odd

Complexity Analysis

Refer to the complete article Check whether a given number is even or odd for more methods.

Article Tags :