Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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.

C++ Program To Check Whether 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++




// 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

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

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


Last Updated : 01 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads