Open In App
Related Articles

Check if a HexaDecimal number is Even or Odd

Improve Article
Improve
Save Article
Save
Like Article
Like

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

Input: N = ABC7787CC87AA
Output: Even

Input: N = 9322DEFCD
Output: Odd

 

Naive Approach: 
 

Time Complexity: O(N)
Efficient approach: Since Hexadecimal numbers contain digits from 0 to 15, therefore we can simply check if the last digit is either ‘0’, ‘2’, ‘4’, ‘6’, ‘8’, ‘A'(=10), ‘C'(=12) or ‘E'(=14). If it is, then the given HexaDecimal number will be Even, else Odd.
Below is the implementation of the above approach. 
 

C++




// C++ code to check if a HexaDecimal
// number is Even or Odd
 
#include <bits/stdc++.h>
using namespace std;
 
// Check if the number is odd or even
string even_or_odd(string N)
{
    int len = N.size();
 
    // check if the last digit
    // is either '0', '2', '4',
    // '6', '8', 'A'(=10),
    // 'C'(=12) or 'E'(=14)
    if (N[len - 1] == '0'
        || N[len - 1] == '2'
        || N[len - 1] == '4'
        || N[len - 1] == '6'
        || N[len - 1] == '8'
        || N[len - 1] == 'A'
        || N[len - 1] == 'C'
        || N[len - 1] == 'E')
        return ("Even");
    else
        return ("Odd");
}
 
// Driver code
int main()
{
    string N = "AB3454D";
 
    cout << even_or_odd(N);
 
    return 0;
}


Java




// Java code to check if a HexaDecimal
// number is Even or Odd
class GFG{
  
// Check if the number is odd or even
static String even_or_odd(String N)
{
    int len = N.length();
  
    // check if the last digit
    // is either '0', '2', '4',
    // '6', '8', 'A'(=10),
    // 'C'(=12) or 'E'(=14)
    if (N.charAt(len - 1) == '0'
        || N.charAt(len - 1) == '2'
        || N.charAt(len - 1) == '4'
        || N.charAt(len - 1) == '6'
        || N.charAt(len - 1) == '8'
        || N.charAt(len - 1) == 'A'
        || N.charAt(len - 1) == 'C'
        || N.charAt(len - 1) == 'E')
        return ("Even");
    else
        return ("Odd");
}
  
// Driver code
public static void main(String[] args)
{
    String N = "AB3454D";
    System.out.print(even_or_odd(N));
}
}
 
// This code is contributed by 29AjayKumar


Python 3




# Python code to check if a HexaDecimal
# number is Even or Odd
 
# Check if the number is odd or even
def even_or_odd(N):
    l = len(N)
 
    # check if the last digit
    # is either '0', '2', '4',
    # '6', '8', 'A'(=10),
    # 'C'(=12) or 'E'(=14)
    if (N[l - 1] == '0'or N[l - 1] == '2'or
        N[l - 1] == '4'or N[l - 1] == '6'or
        N[l - 1] == '8'or N[l - 1] == 'A'or
        N[l - 1] == 'C'or N[l - 1] == 'E'):
        return ("Even")
    else:
        return ("Odd")
 
# Driver code
N = "AB3454D"
 
print(even_or_odd(N))
 
# This code is contributed by Atul_kumar_Shrivastava


C#




// C# code to check if a HexaDecimal
// number is Even or Odd
using System;
 
public class GFG{
 
    // Check if the number is odd or even
    static string even_or_odd(string N)
    {
        int len = N.Length;
     
        // check if the last digit
        // is either '0', '2', '4',
        // '6', '8', 'A'(=10),
        // 'C'(=12) or 'E'(=14)
        if (N[len - 1] == '0'
            || N[len - 1] == '2'
            || N[len - 1] == '4'
            || N[len - 1] == '6'
            || N[len - 1] == '8'
            || N[len - 1] == 'A'
            || N[len - 1] == 'C'
            || N[len - 1] == 'E')
            return ("Even");
        else
            return ("Odd");
    }
     
    // Driver code
    static public void Main ()
    {
        string N = "AB3454D";
     
        Console.WriteLine(even_or_odd(N));
    }
}
 
// This code is contributed by shubhamsingh10


Javascript




<script>
 
// Javascript code to check if a HexaDecimal
// number is Even or Odd
 
    // Check if the number is odd or even
    function even_or_odd(N)
    {
        let len = N.length;
       
        // check if the last digit
        // is either '0', '2', '4',
        // '6', '8', 'A'(=10),
        // 'C'(=12) or 'E'(=14)
        if (N[len - 1] == '0'
            || N[len - 1] == '2'
            || N[len - 1] == '4'
            || N[len - 1] == '6'
            || N[len - 1] == '8'
            || N[len - 1] == 'A'
            || N[len - 1] == 'C'
            || N[len - 1] == 'E')
            return ("Even");
        else
            return ("Odd");
    }
 
// Driver Code
 
    let N = "AB3454D";
       
    document.write(even_or_odd(N));
 
</script>


Output: 

Odd

 

Time complexity: O(1)

Auxiliary Space: O(1)


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 17 Jul, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials