C++ Program to Check Whether Number is Even or Odd
Last Updated :
15 Oct, 2024
A number is even if it is completely divisible by 2 and it is odd if it is not completely divisible by 2. In this article, we will learn how to check whether a number is even or odd in C++.
Examples
Input: n = 11
Output: Odd
Explanation: Since 11 is not completely divisible by 2, it is an odd number.
Input: n = 20
Output: Even
Explanation: Since 20 is completely divisible by 2, it is an even number.
Check if a Number is Even or Odd in C++
The simplest method to determine if a given number is even or odd is by checking the remainder left when the number is divided by 2. The modulo operator (%) in C++ returns remainder after the division. So, using an if-else or any other conditional statement, we check if the remainder of the number divided by 2 is 0. If it is 0, the number is even. Otherwise, the number is odd.
Code Implementation
C++
// C++ program to check if the number is even
// or odd using modulo operator
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 11;
// If n is completely divisible by 2
if (n % 2 == 0)
cout << "Even";
// If n is NOT completely divisible by 2
else
cout << "Odd";
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Other Methods to Check if the Number is Even or Odd
The above method is the simplest method to check the parity of the number. But there are two more different ways to check whether the given number is a prime number or not.
Using Bitwise AND (&) Operator
We can also check the parity of the number just by checking the Least Significant Bit (LSB) of the number. In binary representation of odd numbers, the LSB (0th bit) in an odd number is always set i.e. will always be 1. So, we can extract the LSB by doing bitwise AND (&) with 1 and check whether it is set or not.
C++
// C++ program to check if the number is even
// or odd using bitwise (&) operator
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 11;
// Performing AND operation of n with 1
int res = n & 1;
// If res is 0, the number is even
if (res == 0)
cout << "Even";
// Otherwise, number is odd
else
cout << "Odd";
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Using (<< and >>) Shift Operators
The least significant bit (LSB) in an odd number is always set, i.e., 1. If we right shift the number by one and then left shift it by one, the LSB will become 0, making it unequal to the original number. In contrast, if we perform the same operation on an even number, it won’t change because the LSB is already 0 from the start.
C++
// C++ program to check if the number is even
// or odd using shift (<< and >>) operators
#include <iostream>
using namespace std;
int main() {
int n = 11;
// Variable to store the original number
int temp = n;
// Right shift the number by 1
temp = temp >> 1;
// Left shift the number back by 1
temp = temp << 1;
// Check if the value of the number changed
// or not
if (temp == n) {
cout << "Even" << endl;
} else {
cout << "Odd" << endl;
}
return 0;
}
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
C++ Program To Check Whether The Length Of Given Linked List Is Even Or Odd Given a linked list, the task is to make a function which checks whether the length of the linked list is even or odd. Examples: Input : 1->2->3->4->NULL Output : Even Input : 1->2->3->4->5->NULL Output : OddRecommended: Please solve it on "PRACTICE" first, before moving o
3 min read
C++ Program to Print Even Numbers in an Array Given an array of numbers, the task is to print all the even elements of the array. Let us understand it with few examples mentioned below. Example: Input: num1 = [2,7,6,5,4] Output: 2, 6, 4 Input: num2 = [1,4,7,8,3] Output: 4, 81. Using for loop Approach: Iterate each element in the given using for
5 min read
Check whether count of odd and even factors of a number are equal Given a number N, the task is to find whether N has an equal number of odd and even factors.Examples: Input: N = 10 Output: YES Explanation: 10 has two odd factors (1 and 5) and two even factors (2 and 10)Input: N = 24 Output: NO Explanation: 24 has two odd factors (1 and 3) and six even factors (2,
8 min read
Check if the number is even or odd whose digits and base (radix) is given Given an array arr[] of size N which represents the digits of a number and an integer r which is the base (radix) of the given number i.e. n = arr[n - 1] * r0 + arr[n - 2] * r1 + ... + a[0] * rN - 1. The task is to find whether the given number is odd or even.Examples: Input: arr[] = {1, 0}, r = 2 O
6 min read
Program for n-th even number Given a number n, print the nth even number. The 1st even number is 2, 2nd is 4 and so on. Examples: Input : 3Output : 6First three even numbers are 2, 4, 6, .. Input : 5Output : 10First five even numbers are 2, 4, 6, 8, 19.. The nth even number is given by the formula 2*n. C++ // CPP program to fin
2 min read