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:



Below is the implementation of the above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to check if a Octal
// number is Even or Odd
#include <iostream>
using namespace std;
  
    // To return value of a char.
    int val(char c)
    {
        if (c >= '0' && c <= '9')
            return (int)c - '0';
        else
            return (int)c - 'A' + 10;
    }
  
    // Function to convert a
    // number from N base to decimal
    int toDeci(string str,
                      int base)
    {
        int len = str.length();
  
        // power of base
        int power = 1;
  
        int num = 0;
        int i;
  
        // Decimal equivalent is
        // str[len-1]*1 + str[len-1] *
        // base + str[len-1]*(base^2) + ...
        for (i = len - 1; i >= 0; i--) {
  
            // A digit in input number
            // must be less than
            // number's base
            if (val(str[i]) >= base) {
  
                cout << "Invalid Number";
                return -1;
            }
  
            num += val(str[i])* power;
            power = power * base;
        }
  
        return num;
    }
  
    // Returns true if n is even, else odd
     bool isEven(string num, int N)
    {
  
        int deci = toDeci(num, N);
  
        return (deci % 2 == 0);
    }
  
int main()
{
        string num = "11A";
        int N = 16;
  
        if (isEven(num, N)) {
            cout << "Even";
        }
        else {
            cout << "Odd";
        }
}
  
// This code is contributed by AnkitRai01

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to check if a Octal
// number is Even or Odd
  
import java.io.*;
  
class Main {
  
    // To return value of a char.
    static int val(char c)
    {
        if (c >= '0' && c <= '9')
            return (int)c - '0';
        else
            return (int)c - 'A' + 10;
    }
  
    // Function to convert a
    // number from N base to decimal
    static int toDeci(String str,
                      int base)
    {
        int len = str.length();
  
        // power of base
        int power = 1;
  
        int num = 0;
        int i;
  
        // Decimal equivalent is
        // str[len-1]*1 + str[len-1] *
        // base + str[len-1]*(base^2) + ...
        for (i = len - 1; i >= 0; i--) {
  
            // A digit in input number
            // must be less than
            // number's base
            if (val(str.charAt(i)) >= base) {
  
                System.out.println("Invalid Number");
                return -1;
            }
  
            num += val(str.charAt(i))
                   * power;
            power = power * base;
        }
  
        return num;
    }
  
    // Returns true if n is even, else odd
    public static boolean isEven(
        String num, int N)
    {
  
        int deci = toDeci(num, N);
  
        return (deci % 2 == 0);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String num = "11A";
        int N = 16;
  
        if (isEven(num, N)) {
            System.out.println("Even");
        }
        else {
            System.out.println("Odd");
        }
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to check if a Octal
# number is Even or Odd
  
# To return value of a char.
def val(c):
    if (ord(c) >= ord('0') and ord(c) <= ord('9')):
        return ord(c) - ord('0')
    else:
        return ord(c) - ord('A') + 10
  
# Function to convert a
# number from N base to decimal
def toDeci(str, base):
    Len = len(str)
  
    # power of base
    power = 1
  
    num = 0
  
    # Decimal equivaLent is
    # str[Len-1]*1 + str[Len-1] *
    # base + str[Len-1]*(base^2) + ...
    for i in range(Len-1, -1, -1):
  
        # A digit in input number
        # must be less than
        # number's base
        if (val(str[i]) >= base):
  
            print("Invalid Number")
            return -1
  
        num += val(str[i])*power
        power = power * base
  
    return num
  
# Returns true if n is even, else odd
def isEven(num, N):
  
    deci = toDeci(num, N)
  
    return (deci % 2 == 0)
  
# Driver code
if __name__ == '__main__':
    num = "11A"
    N = 16
  
    if (isEven(num, N)):
        print("Even")
    else:
        print("Odd")
  
# This code is contributed by mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to check if a Octal
// number is Even or Odd
using System;
  
class Gfg{
  
    // To return value of a char.
    static int val(char c)
    {
        if (c >= '0' && c <= '9')
            return (int)c - '0';
        else
            return (int)c - 'A' + 10;
    }
  
    // Function to convert a
    // number from N base to decimal
    static int toDeci(string str,int base_var)
    {
        int len = str.Length;
  
        // power of base
        int power = 1;
  
        int num = 0;
        int i;
  
        // Decimal equivalent is
        // str[len-1]*1 + str[len-1] *
        // base + str[len-1]*(base^2) + ...
        for (i = len - 1; i >= 0; i--) {
  
            // A digit in input number
            // must be less than
            // number's base
            if (val(str[i]) >= base_var) {
  
                Console.WriteLine("Invalid Number");
                return -1;
            }
  
            num += val(str[i])
                   * power;
            power = power * base_var;
        }
  
        return num;
    }
  
    // Returns true if n is even, else odd
    public static bool isEven(
        string num, int N)
    {
  
        int deci = toDeci(num, N);
  
        return (deci % 2 == 0);
    }
  
    // Driver code
    public static void Main(string[] args)
    {
        string num = "11A";
        int N = 16;
  
        if (isEven(num, N)) {
            Console.WriteLine("Even");
        }
        else {
            Console.WriteLine("Odd");
        }
    }
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

Even

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29, AnkitRai01