TCS Coding Question - Base Seventeen - Given a maximum of four digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D … 16 – G} as input

TCS Coding Question - Base Seventeen
Problem Statement
Given a maximum of four digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D 14 - E 15 - F 16 – G} as input, output its decimal value.

 

Test Cases

Case 1

    Input – 1A

    Expected Output – 27


Case 2

    Input – 23GF

    Expected Output – 10980

Explanation:


According to Given problem number with base 17 can be written as 


1 - 1

2 - 2

3 - 3 

4 - 4

5 - 5

6 - 6

7 - 7 

8 - 8

9 - 9 

10 - A

11 - B

12 - C

13 - D

14 - E

15 - F

16 - G 



Therefore 18 (in base 10) can be written as 11 (in base 17) which means (1 * (17)^1) + (1 *(17)^0 ) 


Below is the Code for the Above Question


C Program 

#include <stdio.h>
#include <math.h>
#include <string.h>

int main(){

  char baseSeventeen[17];
  long long decimal, place;

  int i = 0, val, len;
  decimal = 0;
  
  scanf("%s",baseSeventeen);

  len = strlen(baseSeventeen);
  len--;

  for(i = 0;baseSeventeen[i]!='\0';i++)
  {
    if(baseSeventeen[i]>='0'&& baseSeventeen[i]<='9'){

      val = baseSeventeen[i] - 48; // ASCIIvalues of 0 - 9 is 48 to 57
    }
    else if(baseSeventeen[i]>='a'&& baseSeventeen[i]<='g'){

      val = baseSeventeen[i] - 97 + 10; // ASCII values of 'a' to 'g' are 97 to 103
    }
    else if(baseSeventeen[i]>='A'&& baseSeventeen[i]<='G'){

      val = baseSeventeen[i] - 65 + 10; // ASCII value of 'A' to 'G' are 65 to 71
    }
    decimal = decimal + val * pow(17,len);
    len--;
  }
  printf("%lld",decimal);
  return 0;
}


Java Program

import java.util.HashMap;
import java.util.Scanner;

public class BaseSeventeen {

	public static void main(String[] args) {
		
		HashMap<Character, Integer> baseValMap = new HashMap<Character, Integer>();
		// Faster approach to get value from HashMap
		baseValMap.put('A', 10);
		baseValMap.put('B', 11);
		baseValMap.put('C', 12);
		baseValMap.put('D', 13);
		baseValMap.put('E', 14);
		baseValMap.put('F', 15);
		baseValMap.put('G', 16);
		baseValMap.put('a', 10);
		baseValMap.put('b', 11);
		baseValMap.put('c', 12);
		baseValMap.put('d', 13);
		baseValMap.put('e', 14);
		baseValMap.put('f', 15);
		baseValMap.put('g', 16);
		
		Scanner sc = new Scanner(System.in);

		String input = sc.nextLine();
		long num = 0;
		int k = 0;
		
		for (int i = input.length() - 1; i >= 0; i--) {
			
			// Checking if char lies between a to z or A to Z
			if ((input.charAt(i) >= 'A' && input.charAt(i) <= 'Z') 
					|| (input.charAt(i) >= 'a' && input.charAt(i) <= 'z')) {
				
				// if present then use value from  map to calculate value
				num = num + baseValMap.get(input.charAt(i)) * (int) Math.pow(17, k++);
			} else {
				//else simply use numeric value to calculate
				num = num + ((input.charAt(i) - '0') * (int) Math.pow(17, k++));
			}
		}
		System.out.println(num);
	}
}


For More TCS Digital Capability Assessment solution - Click here  or Here


This Blog is Hard work !

Can you give me a treat 😌

Post a Comment

2 Comments