Check if a N base number is Even or Odd Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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 by dividing it with 2, and checking it's remainder. Below is the implementation of the above approach. C++ // 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 Java // 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"); } } } Python3 # 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 C# // 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 JavaScript <script> // JavaScript code to check if a Octal // number is Even or Odd // To return value of a char. function val(c) { if (c >= '0' && c <= '9') return c.charCodeAt() - '0'.charCodeAt(); else return c.charCodeAt() - 'A'.charCodeAt() + 10; } // Function to convert a // number from N base to decimal function toDeci(str, base) { let len = str.length; // power of base let power = 1; let num = 0; let 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) { document.write("Invalid Number"); return -1; } num += val(str[i]) * power; power = power * base; } return num; } // Returns true if n is even, else odd function isEven(num, N) { let deci = toDeci(num, N); return (deci % 2 == 0); } // Driver Code let num = "11A"; let N = 16; if (isEven(num, N)) { document.write("Even"); } else { document.write("Odd"); } </script> Output: Even Time Complexity: O(len) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Check if an Octal number is Even or Odd C code_r Follow Improve Article Tags : Mathematical DSA number-theory Practice Tags : Mathematicalnumber-theory Similar Reads Check if a number is in given base or not Given a number as a string and a base, check if the given number is in the given base or not. Examples: Input : str = "1010", base = 2 Output : Yes Input : str = "1015", base = 2 Output : No Input : str = "AF87", base = 16 Output : Yes The idea is to one by one check if all digits are in the given b 10 min read Check if an Octal number is Even or Odd Given an Octal number N, check whether it is even or odd.Examples: Input: N = 7234 Output: Even Input: N = 333333333 Output: Odd Naive Approach: Convert the number from Octal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity: 4 min read Check if an Octal number is Even or Odd Given an Octal number N, check whether it is even or odd.Examples: Input: N = 7234 Output: Even Input: N = 333333333 Output: Odd Naive Approach: Convert the number from Octal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity: 4 min read Check if an Octal number is Even or Odd Given an Octal number N, check whether it is even or odd.Examples: Input: N = 7234 Output: Even Input: N = 333333333 Output: Odd Naive Approach: Convert the number from Octal base to Decimal base.Then check if the number is even or odd, which can be easily checked by dividing by 2. Time Complexity: 4 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. 4 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. 4 min read Like