Check whether a given number is even or odd
Last Updated :
20 Jun, 2025
Given a number n, check whether it is even or odd. Return true for even and false for odd.
Examples:
Input: n = 15
Output: false
Explanation: 15 % 2 = 1, so 15 is odd .
Input: n = 44
Output: true
Explanation: 44 % 2 = 0, so 44 is even.
[Naive Approach] By Finding the Remainder - O(1) Time and O(1) Space
We can check the remainder when divided by 2. If the remainder is 0, the number is even, otherwise it is odd.
C++
#include <iostream>
using namespace std;
bool isEven(int n)
{
// finding remainder of n
int rem = n % 2;
if(rem == 0){
return true;
}
else{
return false;
}
}
int main()
{
int n = 15;
if (isEven(n))
cout << "true";
else
cout << "false";
return 0;
}
C
#include <stdio.h>
#include <stdbool.h>
bool isEven(int n)
{
return n % 2 == 0;
}
// Driver Code
int main()
{
int n = 15;
if (isEven(n))
printf("true\n");
else
printf("false\n");
return 0;
}
Java
class GfG {
public static boolean isEven(int n)
{
// finding remainder of n
int rem = n % 2;
if(rem == 0){
return true;
}
else{
return false;
}
}
// Driver Code
public static void main(String[] args)
{
int n = 15;
if (isEven(n) == true)
System.out.print("true");
else
System.out.print("false");
}
}
Python
def isEven(n):
# finding remainder of n
rem = n % 2;
if rem == 0:
return True
else:
return False
if __name__ == "__main__":
n = 15
if isEven(n):
print("true")
else:
print("false")
C#
using System;
class Program
{
static bool isEven(int n)
{
// finding remainder of n
int rem = n % 2;
if (rem == 0){
return true;
}
else{
return false;
}
}
static void Main()
{
int n = 15;
if (isEven(n))
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
function isEven(n) {
// finding remainder of n
let rem = n % 2;
if(rem == 0){
return true;
}
else{
return false;
}
}
// Driver code
let n = 15;
if (isEven(n)) {
console.log("true");
}
else {
console.log("false");
}
[Efficient Approach] Using Bitwise AND Operator - O(1) Time and O(1) Space
The last bit of all odd numbers is always 1, while for even numbers it’s 0. So, when performing bitwise AND operation with 1, odd numbers give 1, and even numbers give 0.
Note: Bitwise operators are extremely fast and efficient because they operate directly at the binary level, making them significantly faster than arithmetic or logical operations.
Examples:
15 -> 1 1 1 1
& 0 0 0 1
-------
0 0 0 1 , so this we can say it is an odd number.
44 -> 1 0 1 1 0 0
& 0 0 0 0 0 1
----------
0 0 0 0 0 0 , so this we can say it is an even number.
C++
#include <iostream>
using namespace std;
bool isEven(int n)
{
// taking bitwise and of n with 1
if ((n & 1) == 0)
return true;
else
return false;
}
int main()
{
int n = 15;
if (isEven(n) == true)
cout << "true";
else
cout << "false";
return 0;
}
C
#include <math.h>
#include <stdio.h>
#include <stdbool.h> // for using boolean datatype in c
bool isEven(int n)
{
// taking bitwise and of n with 1
if ((n & 1) == 0)
return true;
else
return false;
}
int main()
{
int n = 15;
if (isEven(n))
{
printf("true");
}
else
{
printf("false");
}
return 0;
}
Java
class GfG {
public static boolean isEven(int n)
{
// taking bitwise and of n with 1
if ((n & 1) == 0)
return true;
else
return false;
}
public static void main(String[] args)
{
int n = 15;
if (isEven(n) == true)
System.out.print("true");
else
System.out.print("false");
}
}
Python
def isEven(n):
# taking bitwise and of n with 1
if (n & 1) == 0:
return True
else:
return False
if __name__ == "__main__":
n = 15
if isEven(n):
print("true")
else:
print("false")
C#
using System;
class GfG {
public static bool isEven(int n)
{
// taking bitwise and of n with 1
if ((n & 1) == 0)
return true;
else
return false;
}
public static void Main()
{
int n = 15;
if (isEven(n) == true)
Console.WriteLine("true");
else
Console.WriteLine("false");
}
}
JavaScript
function isEven(n)
{
// taking bitwise and of n with 1
if ((n & 1) === 0) {
return true;
}
else {
return false;
}
}
// Driver Code
let n = 15;
if (isEven(n)) {
console.log("true");
}
else {
console.log("false");
}
Similar Reads
Check whether given floating point number is even or odd Given a floating-point number, check whether it is even or odd. We can check whether a integer is even or odd by dividing its last digit by 2. But in case of floating point number we can't check a given number is even or odd by just dividing its last digit by 2. For example, 100.70 is an odd number
6 min read
Check whether XOR of all numbers in a given range is even or odd Given a range [ L, R ], the task is to find if the value of XOR of all natural numbers in the range L to R ( both inclusive ) is even or odd. Print 'Even' if XOR of all numbers in the range is even, otherwise print odd. Examples: Input: L = 1, R= 10 Output: Odd Input: L= 5, R=15 Output: Even A Simpl
8 min read
Check whether bitwise AND of N numbers is Even or Odd Given an array arr[] containing N numbers. The task is to check whether the bitwise-AND of the given N numbers is even or odd.Examples: Input: arr[] = { 2, 12, 20, 36, 38 } Output: Even Input: arr[] = { 3, 9, 17, 13, 15 } Output: Odd A Simple Solution is to first find the AND of the given N numbers,
7 min read
Check if an Octal number is Even or Odd Given an Octal number N, check whether it is even or odd.Examples: Input: N = 7234 Output: Even Input: N = 333333333 Output: Odd Naive Approach: Convert the number from Octal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity:
4 min read
Check if a N base number is Even or Odd Given a number num in base N, check whether it is even or odd.Examples: Input: num = 10, N = 8 Output: Even Explanation: 108 = 810, which is even Input: num = 122, N = 5 Output: Odd Explanation: 1225 = 3710, which is odd Approach: Convert the number num from Base N to Decimal base.Check whether the
6 min read