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

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


  • benne saiprakash

    #include
    using namespace std;
    int main(){
    string s;
    cin>>s;
    int n=s.size();
    long long ans=0;
    long long int p=1;
    for(int i=n-1;i>=0;i–){
    if(s[i]>=’A’ && s[i]<='Z'){
    int value= s[i]-'A' + 10;
    ans+=value*p;
    }
    else{
    int value=s[i]-'0';
    ans+=value*p;
    }
    p=p*17;
    }
    cout<<ans<<"\n";
    }


  • Gyanendra

    import java.util.*;
    public class Main
    {
    public static void main(String[] args) {
    Scanner scn=new Scanner(System.in);
    String str=scn.nextLine();

    int val1=10;
    HashMap hs=new HashMap();

    for(int i=65;i=0;i–){

    char ch=str.charAt(i);

    if(ch>=’A’ && ch=’a’ && ch<='z'){
    ch=Character.toUpperCase(ch);

    int val2=hs.get(ch);

    int spo=(int)Math.pow(17,val);
    int temp=val2*spo;
    sum+=temp;
    val++;
    }
    else{
    int temp=ch-'0';
    int spo=(int)Math.pow(17,val);
    sum+=(temp*spo);
    val++;
    }

    }

    System.out.println(sum);

    }
    }


  • 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) ;

    }
    }