TCS Coding Question 2 | Given a maximum of four digit to the base 17 (10 – A …

Sweet Seventeen Problem Statement

Given a maximum of four digit to the base 17 (10 – A, 11 – B, 12 – C, 13 – D … 16 – G} as input, output its decimal value.

Test Cases

Case 1

  • Input – 1A
  • Expected Output – 27

Case 2

  • Input – 23GF
  • Expected Output – 10980
Given a maximum of four digit to the base 17
#include <stdio.h>
#include <math.h>
#include <string.h>

int main(){

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

  int i = 0, val, len;
  decimal = 0;
  place = 1;

  scanf("%s",hex);

  len = strlen(hex);
  len--;

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

      //48 to 57 are ascii values of 0 - 9
      //say value is 8 its ascii will be 56
      //val = hex[i] - 48 => 56 - 48 => val = 8

      val = hex[i] - 48;
    }
    else if(hex[i]>='a'&& hex[i]<='g'){

      //97 to 103 are ascii values of a - g
      //say value is g its ascii will be 103
      //val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16
      //10 is added as g value is 16 not 6 or a value is 10 not 0

      val = hex[i] - 97 + 10;
    }
    else if(hex[i]>='A'&& hex[i]<='G'){

      //similarly, 65 to 71 are values of A - G
      val = hex[i] - 65 + 10;
    }

    decimal = decimal + val * pow(17,len);
    len--;
  }

  printf("%lld",decimal);

  return 0;
}
#include <iostream>
#include <math.h>
#include <string.h>

using namespace std;
int main(){

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

  int i = 0, val, len;
  decimal = 0;
  place = 1;

  cin>> hex;

  len = strlen(hex);
  len--;

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

      //48 to 57 are ascii values of 0 - 9
      //say value is 8 its ascii will be 56
      //val = hex[i] - 48 => 56 - 48 => val = 8

      val = hex[i] - 48;
    }
    else if(hex[i]>='a'&& hex[i]<='g'){

      //97 to 103 are ascii values of a - g
      //say value is g its ascii will be 103
      //val = hex[i] - 97 + 10 => 103 - 97 + 10=> val = 16
      //10 is added as g value is 16 not 6 or a value is 10 not 0

      val = hex[i] - 97 + 10;
    }
    else if(hex[i]>='A'&& hex[i]<='G'){

      //similarly, 65 to 71 are values of A - G
      val = hex[i] - 65 + 10;
    }

    decimal = decimal + val * pow(17,len);
    len--;
  }

  cout<< decimal;

  return 0;
}
'''The int() function converts the specified value into an integer number.
We are using the same int() method to convert the given input.
int() accepts two arguments, number and base.
Base is optional and the default value is 10.
In the following program we are converting to base 17'''

num = str(input())
print(int(num,17))
import java.util.*;
public class Main
{
	public static void main(String[] args) {
		HashMap<Character,Integer> hmap = new HashMap<Character,Integer>();
		hmap.put('A',10);
		hmap.put('B',11);
		hmap.put('C',12);
		hmap.put('D',13);
		hmap.put('E',14);
		hmap.put('F',15);
		hmap.put('G',16);
		hmap.put('a',10);
		hmap.put('b',11);
		hmap.put('c',12);
		hmap.put('d',13);
		hmap.put('e',14);
		hmap.put('f',15);
		hmap.put('g',16);
		Scanner sin = new Scanner(System.in);
		
                String s = sin.nextLine();
		long  num=0;
		int k=0;
		
                for(int i=s.length()-1;i>=0;i--)
		{
		    if((s.charAt(i)>='A'&&s.charAt(i)<='Z')||(s.charAt(i)>='a' &&s.charAt(i)<='z'))
		       {
		           num = num + hmap.get(s.charAt(i))*(int)Math.pow(17,k++);
		       }
		    else
		       {
		        num = num+((s.charAt(i)-'0')*(int)Math.pow(17,k++));
		}
		}
		System.out.println(num);
	}
}

16 comments on “TCS Coding Question 2 | Given a maximum of four digit to the base 17 (10 – A …”


  • apoorv1498

    #include
    using namespace std;

    int main(){
    string s;
    cin>>s;
    int dec = 0;
    int k = s.length()-1;
    for(int i=0; i<s.length(); i++){
    char ch = s[i];
    if(ch=='A'){
    dec = dec + 10*pow(17,k);
    k–;
    }
    else if(ch=='B'){
    dec = dec + 11*pow(17,k);
    k–;
    }
    else if(ch=='C'){
    dec = dec + 12*pow(17,k);
    k–;
    }
    else if(ch=='D'){
    dec = dec + 13*pow(17,k);
    k–;
    }
    else if(ch=='E'){
    dec = dec + 14*pow(17,k);
    k–;
    }
    else if(ch=='F'){
    dec = dec + 15*pow(17,k);
    k–;
    }
    else if(ch=='G'){
    dec = dec + 16*pow(17,k);
    k–;
    }
    else{
    dec = dec + (int(s[i])-48)*pow(17,k);
    k–;
    }
    }
    cout<<dec;
    }


  • Jatin

    /*Little bit lengthy But easy Explanation */
    import java.util.*;
    public class Main {
    public static void main(String args[]) {
    String str = “23GF”;
    HashMaphmap = new HashMap() ;
    hmap.put(‘A’,10) ;
    hmap.put(‘B’,11) ;
    hmap.put(‘C’,12) ;
    hmap.put(‘D’,13) ;
    hmap.put(‘E’,14) ;
    hmap.put(‘F’,15) ;
    hmap.put(‘G’,16) ;
    hmap.put(‘H’,17) ;
    hmap.put(‘0’,0) ;
    hmap.put(‘1’,1) ;
    hmap.put(‘2’,2) ;
    hmap.put(‘3’,3) ;
    hmap.put(‘4’,4) ;
    hmap.put(‘5’,5) ;
    hmap.put(‘6’,6) ;
    hmap.put(‘7’,7) ;
    hmap.put(‘8’,8) ;
    hmap.put(‘9’,9) ;

    int k = 0 ;
    int ans1 = 0;
    for(int i = str.length()-1 ; i>=0 ;i–){
    char ch = str.charAt(i) ;
    if(hmap.containsKey(ch)){
    int ans = hmap.get(ch) ;
    ans1 = ans1 + ans*(int)Math.pow(17,k) ;
    k++ ;
    }
    }
    System.out.print(ans1) ;

    }
    }