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

Check whether a given number is even or odd

Last Updated : 11 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given a number, check whether it is even or odd.

Examples : 

Input: 2 
Output: even

Input: 5
Output: odd
Recommended Practice

One simple solution is to find the remainder after dividing by 2. 

Method 1:

C++




// A simple C++ program to
// check for even or odd
#include <iostream>
using namespace std;
  
// Returns true if n is
// even, else odd
bool isEven(int n) { return (n % 2 == 0); }
  
// Driver code
int main()
{
    int n = 101;
    isEven(n) ? cout << "Even" : cout << "Odd";
  
    return 0;
}


Java




// 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


Python3




# A simple Python3 code
# to check for even or odd
  
# Returns true if n is even, else odd
  
  
def isEven(n):
    return (n % 2 == 0)
  
  
# Driver code
n = 101
print("Even" if isEven(n) else "Odd")
  
# This code is contributed by "Sharad_Bhardwaj".


C#




// C# program to
// check for even or odd
using 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




<?php
// A simple PHP program to 
// check for even or odd
  
// Returns true if n is 
// even, else odd
function isEven($n)
{
    return ($n % 2 == 0);
}
  
// Driver code
$n = 101;
if(isEven($n) == true)
    echo "Even";
    else
    echo "Odd";
  
// This code is contributed by Ajit
?>


Javascript




<script>
  
// A simple Javascript program to
// check for even or odd
  
  
// Returns true if n is
// even, else odd
function 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)

Method 2 :

A better solution is to use bitwise operators. We need to check whether the last bit is 1 or not. If the last bit is 1 then the 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++




// A simple C++ program to
// check for even or odd
#include <iostream>
using namespace std;
  
// Returns true if n is
// even, else odd
bool isEven(int n)
{
  
    // n & 1 is 1, then
    // odd, else even
    return (!(n & 1));
}
  
// Driver code
int main()
{
    int n = 101;
    isEven(n) ? cout << "Even" : cout << "Odd";
  
    return 0;
}


C




#include <math.h>
#include <stdio.h>
  
int main()
{
    int n = 101;
    if (n % 2 == 0) {
        printf("Even");
    }
    else {
        printf("Odd");
    }
    return 0;
}


Java




// 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


Python3




# A Python3 code program
# to check for even or odd
  
# Returns true if n is even, else odd
  
  
def isEven(n):
  
    # n&1 is 1, then odd, else even
    return (not(n & 1))
  
  
# Driver code
n = 101
print("Even" if isEven(n) else "Odd")
  
# This code is contributed by "Sharad_Bhardwaj".


C#




// C# program  to
// check for even or odd
using 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




<?php
// A simple PHP program to 
// check for even or odd
   
// Returns true if n is 
// even, else odd
function isEven($n)
{
    return (!($n & 1));
}
   
// Driver code
$n = 101;
if(isEven($n) == true)
    echo "Even";
else
    echo "Odd";
   
// This code is contributed by Smitha
?>


Javascript




<script>
// A simple JavaScript program to
// check for even or odd
  
// Returns true if n is
// even, else odd
function isEven(n)
{
      
    // n & 1 is 1, then
    // odd, else even
    return (!(n & 1));
}
  
// Driver code
let 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)

Method 3: Another approach is by using bitwise left-shift and right-shift operators. The logic behind this implementation is about regenerating the value after the right shift and left shift. We all know even numbers have zero as the last bit and odd have one as the last bit. When we bitwise right shift any number then the last bit of the number piped out whenever it is even or odd. Next, we did a bitwise left shift, then our bit shifted by one. Now last bit placed is empty which is by default filled by a zero. During all these odd numbers changed their value but even remains the same. That’s how by comparing the initial and final value we decide number is even or odd.

 

below is the implementation of the above logic.

C++




#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    int a = 4;
    // To take input from user, prefer below code
    // int a ;
    // cout<<"Enter an number: " <<"\n";
    // cin>>a;
  
    if (a == (a >> 1) << 1) {
        cout << "Number is Even: " << a << endl;
    }
    else {
        cout << "Number is Odd: " << a << endl;
    }
  
    // below is short hand
    // a==(a>>1)<<1? printf("Number %d is
    // even.\n",a):printf("Number %d is odd.\n",a);
  
    return 0;
}


Java




// Java implementation of above approach
import java.io.*;
class HelloWorld {
    public static void main(String[] args)
    {
        int a = 4;
  
        // To take input from user, prefer below code
        // int a ;
        // printf("Enter an number: \n");
        // scanf("%d",&a;);
        if (a == (a >> 1) << 1) {
            System.out.printf("Number %d is even.\n", a);
        }
        else {
            System.out.printf("Number %d is not even.\n",
                              a);
        }
  
        // below is short hand
        // a==(a>>1)<<1? printf("Number %d is
        // even.\n",a):printf("Number %d is odd.\n",a);
    }
}
  
// This code is contributed by karandeep1234.


Python3




a = 4
if a == (a >> 1) << 1:
    print("Number is Even:", a)
else:
    print("Number is Odd:", a)


C#




// C# implementation of above approach
using System;
  
class HelloWorld {
    public static void Main(string[] args)
    {
        int a = 4;
  
        if (a == (a >> 1) << 1) {
            Console.WriteLine("Number " + a + " is even.");
        }
        else {
            Console.WriteLine("Number " + a
                              + " is not even.");
        }
  
        // below is short hand
        // a==(a>>1)<<1? printf("Number %d is
        // even.\n",a):printf("Number %d is odd.\n",a);
    }
}
  
// This code is contributed by karandeep1234.


Javascript




const a = 4;
  
// To take input from user, prefer below code
// let a = prompt("Enter a number: ");
  
// Check if the number is even
if (a == (a >> 1) << 1) {
    console.log("Number is Even: " + a);
}
else {
    console.log("Number is Odd: " + a);
}
  
// This code is contributed by Shivam Tiwari


Output

Number 4 is even.
Number is Even: 4
 

Time Complexity: O(1)
Auxiliary Space: O(1)

This article is contributed by Prabhat Raushan.



Previous Article
Next Article

Similar Reads

Check if a number has an odd count of odd divisors and even count of even divisors
Given an integer N, the task is to check if N has an odd number of odd divisors and even number of even divisors. Examples: Input: N = 36Output: YesExplanation:Divisors of 36 = 1, 2, 3, 4, 6, 9, 12, 18, 36Count of Odd Divisors(1, 3, 9) = 3 [Odd]Count of Even Divisors(2, 4, 6, 12, 18, 36) = 6 [Even] Input: N = 28Output: No Naive Approach: The idea i
9 min read
Count of integers in a range which have even number of odd digits and odd number of even digits
Given a range [L, R], the task is to count the numbers which have even number of odd digits and odd number of even digits. For example, 8 has 1 even digit and 0 odd digit - Satisfies the condition since 1 is odd and 0 is even.545 has 1 even digit and 2 odd digits - Satisfies the condition since 1 is odd and 2 is even.4834 has 3 even digits and 1 od
11 min read
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 but its last digit is divisible by 2. Examples : I
6 min read
Count numbers from given range having odd digits at odd places and even digits at even places
Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively. Examples: Input: L = 3, R = 25Output: 9Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18. Input: L = 128, R = 162Output: 7Explanation: The numbers sa
15+ min read
Absolute difference between sum of even elements at even indices & odd elements at odd indices in given Array
Given an array arr[] containing N elements, the task is to find the absolute difference between the sum of even elements at even indices & the count of odd elements at odd indices. Consider 1-based indexing Examples: Input: arr[] = {3, 4, 1, 5}Output: 0Explanation: Sum of even elements at even indices: 4 {4}Sum of odd elements at odd indices: 4
5 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 Simple Solution is to calculate XOR of all numbers in r
8 min read
Javascript 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->NULLOutput : EvenInput : 1->2->3->4->5->NULLOutput : OddMethod 1: Count the codes linearly Traverse the entire Linked List and keep counting the number of nodes. As soon as t
3 min read
Check whether product of digits at even places is divisible by sum of digits at odd place of a number
Given a number N and numbers of digits in N, the task is to check whether the product of digits at even places of a number is divisible by sum of digits at odd place. If it is divisible, output "TRUE" otherwise output "FALSE". Examples: Input: N = 2157 Output: TRUESince, 1 * 7 = 7, which is divisible by 2+5=7Input: N = 1234Output: TRUESince, 2 * 4
13 min read
Modify Binary Tree by replacing all nodes at even and odd levels by their nearest even or odd perfect squares respectively
Given a Binary Tree consisting of N nodes, the task is to replace all the nodes that are present at even-numbered levels in a Binary Tree with their nearest even perfect square and replace nodes at odd-numbered levels with their nearest odd perfect square. Examples: Input: 5 / \ 3 2 / \ 16 19 Output: 9 / \ 4 4 / \ 9 25 Explanation: Level 1: Nearest
13 min read
Find the nearest odd and even perfect squares of odd and even array elements respectively
Given an array arr[ ] of size N, the task for each array element is to print the nearest perfect square having same parity. Examples: Input: arr[ ] = {6, 3, 2, 15}Output: 4 1 4 9Explanation:The nearest even perfect square of arr[0] (= 6) is 4.The nearest odd perfect square of arr[1] (= 3) is 1.The nearest even perfect square of arr[2] (= 2) is 4The
5 min read
Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).Examples : Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 12 1 8 5 Input : arr[] = {10, 9, 7, 18, 13, 19, 4,
11 min read
Check whether bitwise OR of N numbers is Even or Odd
Given an array arr[] containing N numbers. The task is to check whether the bitwise-OR of the given N numbers is even or odd. Examples: Input : arr[] = { 2, 12, 20, 36, 38 }Output : Even Bit-wise OR Input : arr[] = { 3, 9, 12, 13, 15 }Output : Odd Bit-wise OR A Simple Solution is to first find the OR of the given N numbers, then check if this OR is
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, then check if this AND is even or odd. C/C++ Code
7 min read
Queries to check whether bitwise AND of a subarray is even or odd
Given an array arr[] of N positive integers, the task is to answer Q queries where each query consists of a range [L, R] and you have to check whether the bitwise AND of the elements from the given index range is even or odd.Examples: Input: arr[] = {1, 1, 2, 3}, Q[][] = {{1, 2}, {0, 1}} Output: Even Odd (1 & 2) = 0 which is even. (1 & 1) =
10 min read
Check whether product of 'n' numbers is even or odd
Given an array arr[] containing n numbers. The problem is to check whether the product of the given n numbers is even or odd. Examples: Input: arr[] = {2, 4, 3, 5}Output: EvenExplanation: Product = 2 * 4 * 3 * 5 = 120, 120 is even. Input: arr[] = {3, 9, 7, 1}Output: Odd Recommended: Please try your approach on {IDE} first, before moving on to the s
12 min read
Maximize the difference of sum of elements at even indices and odd indices by shifting an odd sized subarray to end of given Array.
Given an array arr[] of size N, the task is to maximize the difference of the sum of elements at even indices and elements at odd indices by shifting any subarray of odd length to the end of the array. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}Output: 3Explanation: Initially sum of elements at even indices = 1 + 3 + 5 = 9Sum of elements at odd ind
13 min read
Make all the elements of array odd by incrementing odd-indexed elements of odd-length subarrays
Given an array arr[] of size N, the task is to make all the array elements odd by choosing an odd length subarray of arr[] and increment all odd positioned elements by 1 in this subarray. Print the count of such operations required. Examples: Input: arr[] = {2, 3, 4, 3, 5, 3, 2}Output: 2Explanation:In first operation, choose the subarray {2, 3, 4}
9 min read
Check whether sum of digits at odd places of a number is divisible by K
Given two integers 'N' and 'K', the task is to find the sum of digits of 'N' at its odd places (right to left) and check whether the sum is divisible by 'K'. If it is divisible, output YES, otherwise output NO. Examples: Input: N = 4325, K = 4 Output: YES Since, 3 + 5 = 8, which is divisible by 4. Input: N = 1209, K = 3 Output: NO Approach: Find th
7 min read
Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Javascript Program for Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
3 min read
Check whether all the rotations of a given number is greater than or equal to the given number or not
Given an integer x, the task is to find if every k-cycle shift on the element produces a number greater than or equal to the same element. A k-cyclic shift of an integer x is a function that removes the last k digits of x and inserts them in its beginning. For example, the k-cyclic shifts of 123 are 312 for k=1 and 231 for k=2. Print Yes if the giv
12 min read
Check whether product of digits at even places of a number is divisible by K
Given a number N, the task is to check whether the product of digits at even places of a number is divisible by K. If it is divisible, output "YES" otherwise output "NO". Examples: Input: N = 5478, K = 5 Output: YES Since, 5 * 7 = 35, which is divisible by 5 Input: N = 19270, K = 2 Output: NO Approach: Find product of digits at even places from rig
7 min read
Smallest odd number with even sum of digits from the given number N
Given a large number in form of string str. The task is to find the smallest odd number whose sum of digits is even by removing zero or more characters from the given string str, where the digits can be rearranged. Examples Input: str = "15470" Output: 15 Explanation: Two smallest odd digits are 1 & 5. Hence the required number is 15. Input: st
6 min read
Count N-digits numbers made up of even and prime digits at odd and even positions respectively
Given a positive integer N, the task is to find the number of integers of N digits having even digits at odd indices and prime digits at even indices. Examples: Input: N = 2Output: 20Explanation:Following are the possible number of 2-digits satisfying the given criteria {20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 50, 52, 54, 56, 58, 70, 72, 74, 76, 78
12 min read
Check if a given string is Even-Odd Palindrome or not
Given a string str, the task is to check if the given string is Even-Odd Palindrome or not. An Even-Odd Palindrome string is defined to be a string whose characters at even indices form a Palindrome while the characters at odd indices also form a Palindrome separately. Examples: Input: str="abzzab" Output:YES Explanation: String formed by character
7 min read
Check a number is odd or even without modulus operator
Given a number, check whether it is even or odd. Examples : Input: n = 11 Output: Odd Input: n = 10 Output: Even Method 1: Using Loop. The idea is to start with a boolean flag variable as true and switch it n times. If flag variable gets original value (which is true) back, then n is even. Else n is false. Below is the implementation of this idea.
8 min read
Check if product of digits of a number at even and odd places is equal
Given an integer N, the task is to check whether the product of digits at even and odd places of a number are equal. If they are equal, print Yes otherwise print No. Examples: Input: N = 2841 Output: Yes Product of digits at odd places = 2 * 4 = 8 Product of digits at even places = 8 * 1 = 8 Input: N = 4324 Output: No Product of digits at odd place
9 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 number is Odd or Even. This can be easily checked
6 min read
Check if a Number is Odd or Even using Bitwise Operators
Given a number N, the task is to check whether the number is even or odd using Bitwise Operators. Examples: Input: N = 11 Output: Odd Input: N = 10 Output: Even Following Bitwise Operators can be used to check if a number is odd or even: 1. Using Bitwise XOR operator: The idea is to check whether the last bit of the number is set or not. If the las
11 min read
Check if a HexaDecimal number is Even or Odd
Given a HexaDecimal number, check whether it is even or odd.Examples: Input: N = ABC7787CC87AA Output: Even Input: N = 9322DEFCD Output: Odd Naive Approach: Convert the number from Hexadecimal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity: O(N)Efficient approach: Since He
4 min read
three90RightbarBannerImg