Write A Program To Convert A Decimal Number In The Base 17 To A Decimal Number In Base 10. Tcs Nqt

Wed, 14 Apr, 2021

Problem statement
  
 Apply decimal conversion logic to solve this problem, where 0 - 9 digits are same, while replace A B C D E F G with 10 11 12 13 14 15 16 respectively in the given input and do the conversion accordingly
  
 Example-1
 Input: 1A
 Expected Output: 27
  
 Example-2
 Input: 23GF
 Expected Output: 10980
  
  
 PROGRAM IN C++ :

 

#include <iostream>
#include <cstring>

using namespace std;


int val(char c) 
{ 
	if (c >= '0' && c <= '9') 
		return (int)c - '0'; 
	else
		return (int)c - 'A' + 10; 
} 


int toDeci(char *str) 
{      
      int base = 17 ;
	int len = strlen(str); 
	int power = 1; 
	int num = 0; 
	int i; 


	for (i = len - 1; i >= 0; i--) 
	{ 
	  num += val(str[i]) * power; 
	  power = power * base; 
	} 

	return num; 
} 

int main() 
{ 
	char str[50]; 
	cin>>str;
        cout<<toDeci(str); 
	return 0; 
}

 

0 comments
Leave a comment