Write a program that prompts the user to enter a multidigit

Write a program that prompts the user to enter a multi-digit hexadecimal (i.e., base 16) value as a string. The program should output the equivalent decimal value For example, if the user enters 11 then the correct output would be 17. If the user enters IF the correct output would be 31 For an input of 20 the correct output would be 32. Finally, for FF the correct output would be 255 (Do not call Integer.parseInt or Integer.valueOf in your solution)

Solution

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

int main(){
int hex[20], dec, i, j, ch, p;
i=p=dec=0;
printf(\"Enter a Hexadecimal number : \");
while((ch=getchar()) != \'\ \'){
   if((ch > 47 && ch < 58) || (ch > 64 && ch < 71))
       hex[i++] = ch;
}
for(j = i-1; j >= 0; j-- ){
   if(hex[j] > 57)
       dec += (hex[j] - 55) * (int)pow((double)16, p);
   else
       dec += (hex[j] - 48) * (int)pow((double)16, p);
   p++;
}
printf(\"Decimal number is : %d\ \",dec);
getchar();
return 0;
}

 Write a program that prompts the user to enter a multi-digit hexadecimal (i.e., base 16) value as a string. The program should output the equivalent decimal va

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site