Check whether a given number is even or odd - GeeksforGeeks

10 May, 2022
Given a number, check whether it is even or odd.
Examples : 
Input: 2 
Output: even

Input: 5
Output: odd
One simple solution is to find the remainder after dividing by 2. 
C++
Java
Python3
C#
PHP
Javascript
// A simple C++ program to// check for even or odd#include <iostream>using namespace std; // Returns true if n is// even, else oddbool isEven(int n) { return (n % 2 == 0); } // Driver codeint main(){    int n = 101;    isEven(n) ? cout << "Even" : cout << "Odd";     return 0;}
// Java program  to// check for even or odd class GFG{    // Returns true if n is even, else odd    public static boolean isEven(int n)    {        return (n % 2 == 0);    }         // Driver code    public static void main(String[] args)    {        int n = 101;        if(isEven(n) == true)            System.out.print("Even");        else            System.out.print("Odd");    }} // This code is contributed by rishabh_jain
# A simple Python3 code# to check for even or odd # Returns true if n is even, else odddef isEven(n):    return (n % 2 == 0)     # Driver coden = 101print("Even" if isEven(n) else "Odd") # This code is contributed by "Sharad_Bhardwaj".
// C# program to// check for even or oddusing System; class GFG{    // Returns true if n is even, else odd    public static bool isEven(int n)    {        return (n % 2 == 0);    }         // Driver code    public static void Main()    {        int n = 101;        if(isEven(n) == true)            Console.WriteLine("Even");        else            Console.WriteLine("Odd");    }} // This code is contributed by vt_m
<?php// A simple PHP program to// check for even or odd // Returns true if n is// even, else oddfunction isEven($n){    return ($n % 2 == 0);} // Driver code$n = 101;if(isEven != true)    echo "Even";    else    echo "Odd"; // This code is contributed by Ajit?>
<script> // A simple Javascript program to// check for even or odd  // Returns true if n is// even, else oddfunction isEven(n) { return (n % 2 == 0); } // Driver code     let n = 101;    isEven(n) ? document.write("Even") :document.write("Odd");  // This code is contributed by Mayank Tyagi </script>
Output : 
Odd
Time Complexity: O(1)
Auxiliary Space: O(1)A better solution is to use bitwise operators. We need to check whether last bit is 1 or not. If last bit is 1 then number is odd, otherwise always even.Explanation: 
 input : 5    // odd
   00000101              
 & 00000001                
--------------                
   00000001       
--------------

input : 8     //even
   00001000              
 & 00000001                 
--------------               
   00000000        
--------------
Below is the implementation of the idea. 
C++
Java
Python3
C#
PHP
Javascript
// A simple C++ program to// check for even or odd#include <iostream>using namespace std; // Returns true if n is// even, else oddbool isEven(int n){     // n & 1 is 1, then// odd, else evenreturn (!(n & 1));} // Driver codeint main(){int n = 101;isEven(n)? cout << "Even" :           cout << "Odd"; return 0;}
// Java program to// check for even or odd class GFG{    // Returns true if n    // is even, else odd    public static boolean isEven(int n)    {        if((n & 1) == 0)            return true;        else            return false;    }         // Driver code    public static void main(String[] args)    {        int n = 101;        if(isEven(n) == true)            System.out.print("Even");        else            System.out.print("Odd");    }} // This code is contributed by rishabh_jain
# A Python3 code program# to check for even or odd # Returns true if n is even, else odddef isEven(n):         # n&1 is 1, then odd, else even    return (not(n & 1))     # Driver coden = 101;print("Even" if isEven(n) else "Odd") # This code is contributed by "Sharad_Bhardwaj".
// C# program  to// check for even or oddusing System; class GFG{    // Returns true if n    // is even, else odd    public static bool isEven(int n)    {        if((n & 1) == 0)            return true;        else            return false;    }         // Driver code    public static void Main()    {        int n = 101;        if(isEven(n) == true)            Console.WriteLine("Even");        else            Console.WriteLine("Odd");    }} // This code is contributed by vt_m.
<?php// A simple PHP program to// check for even or odd  // Returns true if n is// even, else oddfunction isEven($n){    return (!($n & 1));}  // Driver code$n = 101;if(isEven($n) == true)    echo "Even";else    echo "Odd";  // This code is contributed by Smitha?>
<script>// A simple JavaScript program to// check for even or odd // Returns true if n is// even, else oddfunction isEven(n){         // n & 1 is 1, then    // odd, else even    return (!(n & 1));} // Driver codelet n = 101;isEven(n)? document.write("Even") :        document.write("Odd"); // This code is contributed by Manoj.</script>
Output : 
Odd 
Time Complexity: O(1)
Auxiliary Space: O(1)
This article is contributed by Prabhat Raushan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vt_m
jit_t
Smitha Dinesh Semwal
subham348
mayanktyagi1709
mank1083
subhammahato348
sweetyty
Bit Magic
Mathematical
Sorting
Strings
Strings
Mathematical
Bit Magic
Sorting
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Cyclic Redundancy Check and Modulo-2 Division
Binary representation of a given number
Program to find whether a given number is power of 2
Add two numbers without using arithmetic operators
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7