Open In App

Check whether a given number is even or odd

Last Updated : 13 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a number n, check whether it is even or odd. Return true for even and false for odd.

Examples: 

Input: 2
Output: true

Input: 5
Output: false

Approach: By Finding the Remainder

We can check the remainder when divided by 2. If the remainder is 0, the number is even; otherwise, it is odd

C++
// A simple C++ program to check for even or odd
#include <iostream>
using namespace std;

bool isEven(int n) { 
  return (n % 2 == 0); 
}

int main() {
    int n = 101;
    if(isEven(n))
     	cout << "true";
  	else 
    	cout << "false";

    return 0;
}
C
// A simple C program to check for even or odd
#include <stdio.h>

int isEven(int n) {
    return (n % 2 == 0);
}

int main() {
    int n = 101;
    if(isEven(n))
        printf("true");
    else 
        printf("false");

    return 0;
}
Java
// Java program  to check for even or odd
class GfG {

    public static boolean isEven(int n)  {
        return (n % 2 == 0);
    }

    public static void main(String[] args) {
        int n = 101;
        if (isEven(n) == true)
            System.out.print("true");
        else
            System.out.print("false");
    }
}
Python
# A simple Python3 code check for even or odd

def isEven(n):
    return (n % 2 == 0)


if __name__ == "__main__":
  n = 101
  if is_even(n):
      print("true")
  else:
      print("false")
C#
// C# program toif __name__ == "__main__":
using System;
class GfG {
    public static bool isEven(int n) {
        return (n % 2 == 0);
    }

    public static void Main() {
        int n = 101;
        if (isEven(n) == true)
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
// A simple Javascript program to
// check for even or odd

function isEven(n) { 
	return (n % 2 == 0); 
}

// Driver code

    let n = 101;
    if (isEven(n)) {
        console.log("true");
    } else {
        console.log("false");
    }

Output
false

Time Complexity - O(1)
Space Complexity - O(1)

Approach: Using Bitwise AND Operator

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.

Ex: 5 (101) -> 101
& 001
----
001 , so this we can say it is an odd number.

C++
// A simple C++ program to check for even or odd
#include <iostream>
using namespace std;

bool isEven(int n) {
        if ((n & 1) == 0)
            return true;
        else
            return false;
}

int main() {
    int n = 101;
     if (isEven(n) == true) 
        cout<<"true"; 
  	 else
        cout<<"false"; 

    return 0;
}
C
#include <math.h>
#include <stdio.h>

bool isEven(int n) {
         if ((n & 1) == 0)
            return true;
        else
            return false;
}

int main() {
    int n = 101;
    if (isEven) {
        printf("true");
    }
    else {
        printf("false");
    }
    return 0;
}
Java
// Java program to
// check for even or odd
class GfG {
    public static boolean isEven(int n){
        if ((n & 1) == 0)
            return true;
        else
            return false;
    }

    public static void main(String[] args) {
        int n = 101;
        if (isEven(n) == true)
            System.out.print("true");
        else
            System.out.print("false");
    }
}
Python
# A Python3 code program
# to check for even or odd
def isEven(n):

    # n&1 is 1, then odd, else even
    if (n & 1) == 0:
    	return True
    else:
    	return False

if __name__ == "__main__":
  n = 101
  if isEven(n):
      print("true")
  else:
      print("false")
C#
// C# program  to check for even or odd
using System;

class GfG {
    public static bool isEven(int n)   {
        if ((n & 1) == 0)
            return true;
        else
            return false;
    }
  
    public static void Main() {
        int n = 101;
        if (isEven(n) == true)
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
// A simple JavaScript program to
// check for even or odd

function isEven(n) {
    
 // n & 1 is 1, then odd, else even
 if ((n & 1) === 0) {
        return true;
    } else {
        return false;
    }
}

// Driver code
let n = 101;
if (isEven(n)) {
    console.log("true");
} else {
    console.log("false");
}

Output
false

Time Complexity - O(1)
Space Complexity - O(1)

Approach: Using Bitwise Shift Operators

Right shifting n >> 1 removes the last bit. Shifting back restores it. If the result matches the original number, it's even.

even_odd
C++
#include <bits/stdc++.h>
using namespace std;

bool isEven(int n) {
  if (n == (n >> 1) << 1) {
        return true;
    }
    else {
        return false;
    }
}

int main() {
  
  int n = 4;
  if (isEven(n) == true) 
        cout<<"true"; 
   else
        cout<<"false"; 

    return 0;
}
C
#include <stdio.h>

int isEven(int n) {
    if (n == (n >> 1) << 1) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    int n = 4;
    if (isEven(n) == 1)
        printf("true");
    else
        printf("false");

    return 0;
}
Java
// Java program to
// check for even or odd
class GfG {
    public static boolean isEven(int n){
       if (n == (n >> 1) << 1) 
            return true;
        else 
            return false;
    }

    public static void main(String[] args) {
        int n = 4;
        if (isEven(n) == true)
            System.out.print("true");
        else
            System.out.print("false");
    }
}
Python
# A Python3 code program
# to check for even or odd
def isEven(n):

    if n == (n >> 1) << 1:
    	return True
    else:
    	return False

if __name__ == "__main__":
  n = 4
  if isEven(n):
      print("true")
  else:
      print("false")
C#
// C# program  to check for even or odd
using System;

class GfG {
    public static bool isEven(int n)   {
        if (n == (n >> 1) << 1) 
            return true;
        else
            return false;
    }
  
    public static void Main() {
        int n = 4;
        if (isEven(n) == true)
            Console.WriteLine("true");
        else
            Console.WriteLine("false");
    }
}
JavaScript
// A simple JavaScript program to
// check for even or odd

function isEven(n) {
   
    if (n == (n >> 1) << 1) 
       return true;
    else 
       return false;
}

// Driver code
let n = 4;
if (isEven(n)) {
    console.log("true");
} else {
    console.log("false");
}

Output
true

Time Complexity - O(1)
Space Complexity - O(1)


Next Article

Similar Reads