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++
#include <iostream>
using namespace std;
bool isEven( int n) { return (n % 2 == 0); }
int main()
{
int n = 101;
if (isEven(n))
cout << "Even" ;
else
cout << "Odd" ;
return 0;
}
|
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
Share your thoughts in the comments
Please Login to comment...