Check if a N base number is Even or Odd
Last Updated :
24 Nov, 2021
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++
#include <iostream>
using namespace std;
int val( char c)
{
if (c >= '0' && c <= '9' )
return ( int )c - '0' ;
else
return ( int )c - 'A' + 10;
}
int toDeci(string str,
int base)
{
int len = str.length();
int power = 1;
int num = 0;
int i;
for (i = len - 1; i >= 0; i--) {
if (val(str[i]) >= base) {
cout << "Invalid Number" ;
return -1;
}
num += val(str[i])* power;
power = power * base;
}
return num;
}
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" ;
}
}
|
Java
import java.io.*;
class Main {
static int val( char c)
{
if (c >= '0' && c <= '9' )
return ( int )c - '0' ;
else
return ( int )c - 'A' + 10 ;
}
static int toDeci(String str,
int base)
{
int len = str.length();
int power = 1 ;
int num = 0 ;
int i;
for (i = len - 1 ; i >= 0 ; i--) {
if (val(str.charAt(i)) >= base) {
System.out.println( "Invalid Number" );
return - 1 ;
}
num += val(str.charAt(i))
* power;
power = power * base;
}
return num;
}
public static boolean isEven(
String num, int N)
{
int deci = toDeci(num, N);
return (deci % 2 == 0 );
}
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
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
def toDeci( str , base):
Len = len ( str )
power = 1
num = 0
for i in range ( Len - 1 , - 1 , - 1 ):
if (val( str [i]) > = base):
print ( "Invalid Number" )
return - 1
num + = val( str [i]) * power
power = power * base
return num
def isEven(num, N):
deci = toDeci(num, N)
return (deci % 2 = = 0 )
if __name__ = = '__main__' :
num = "11A"
N = 16
if (isEven(num, N)):
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
class Gfg{
static int val( char c)
{
if (c >= '0' && c <= '9' )
return ( int )c - '0' ;
else
return ( int )c - 'A' + 10;
}
static int toDeci( string str, int base_var)
{
int len = str.Length;
int power = 1;
int num = 0;
int i;
for (i = len - 1; i >= 0; i--) {
if (val(str[i]) >= base_var) {
Console.WriteLine( "Invalid Number" );
return -1;
}
num += val(str[i])
* power;
power = power * base_var;
}
return num;
}
public static bool isEven(
string num, int N)
{
int deci = toDeci(num, N);
return (deci % 2 == 0);
}
public static void Main( string [] args)
{
string num = "11A" ;
int N = 16;
if (isEven(num, N)) {
Console.WriteLine( "Even" );
}
else {
Console.WriteLine( "Odd" );
}
}
}
|
Javascript
<script>
function val(c)
{
if (c >= '0' && c <= '9' )
return c.charCodeAt() - '0' .charCodeAt();
else
return c.charCodeAt() - 'A' .charCodeAt() + 10;
}
function toDeci(str, base)
{
let len = str.length;
let power = 1;
let num = 0;
let i;
for (i = len - 1; i >= 0; i--) {
if (val(str[i]) >= base) {
document.write( "Invalid Number" );
return -1;
}
num += val(str[i])
* power;
power = power * base;
}
return num;
}
function isEven(num, N)
{
let deci = toDeci(num, N);
return (deci % 2 == 0);
}
let num = "11A" ;
let N = 16;
if (isEven(num, N)) {
document.write( "Even" );
}
else {
document.write( "Odd" );
}
</script>
|
Time Complexity: O(len)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...