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 Oddimport 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 decimaldef 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 odddef isEven(num, N): deci = toDeci(num, N) return (deci % 2 == 0)# Driver codeif __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 Oddusing 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> |
Even
Time Complexity: O(len)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Here you will find 19801 additional Information to that Topic: geeksforgeeks.org/check-if-a-n-base-number-is-even-or-odd/ […]
… [Trackback]
[…] Information on that Topic: geeksforgeeks.org/check-if-a-n-base-number-is-even-or-odd/ […]