The Wayback Machine - https://web.archive.org/web/20250114222205/https://www.geeksforgeeks.org/check-whether-given-number-even-odd/
Open In App

Check whether a given number is even or odd

Last Updated : 19 Dec, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

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++
// 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;
}
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

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.

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)
Auxiliary Space: O(1)

Using Bitwise Shift Operators – O(1) Time and O(1) Space

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;
}
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

Unlock your potential with our DSA Self-Paced course, designed to help you master Data Structures and Algorithms at your own pace. In 90 days, you’ll learn the core concepts of DSA, tackle real-world problems, and boost your problem-solving skills, all at a speed that fits your schedule. With comprehensive lessons and practical exercises, this course will set you up for success in technical interviews and beyond.

And here's the challenge – join the Three 90 Challenge! Complete 90% of the course in 90 days, and you’ll earn a 90% refund. It's a great way to stay motivated, track your progress, and reward yourself for your dedication. Start the challenge today, and push your limits to achieve mastery in DSA!


Next Article

Similar Reads

three90RightbarBannerImg