Mega Code Archive

 
Categories / C / Beginners
 

Program to display the decimal equivalent of an input hexadecimal number

(Hexadecimal to Decimal) #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <math.h> void htod(char b[]); int c; void main() { char a[3]; int j; clrscr(); printf("How many digits in input? "); scanf("%d",&c); printf(" Digits of your i/p no. must not exceed entered limit or 3(max)."); printf(" Enter a hexadecimal no."); printf(" No. will be accepted if you press ENTER after inputting no."); printf(" NUMBER:- "); scanf("%s",a); htod (a); getch(); } void htod(char b[]) { int i,e,n,k=0,t=0,s=0; n=*b; for (i=c-1;i>=0;--i) { e =(c-1)-i; if (i>0) *b=*(b+i); else *b=n; k = pow(16,e); if (*b =='1') { t=1*k; s=s+t; } else if (*b=='2') { t=2*k; s=s+t; } else if (*b=='3') { t=3*k; s=s+t; } else if (*b=='4') { t=4*k; s=s+t; } else if (*b=='5') { t=5*k; s=s+t; } else if (*b=='6') { t=6*k; s=s+t; } else if (*b=='7') { t=7*k; s=s+t; } else if (*b=='8') { t=8*k; s=s+t; } else if (*b=='9') { t=9*k; s=s+t; } else if (*b=='a'||*b=='A') { t=10*k; s=s+t; } else if (*b=='b'||*b=='B') { t=11*k; s=s+t; } else if (*b=='c'||*b=='C') { t=12*k; s=s+t; } else if (*b=='d'||*b=='D') { t=13*k; s=s+t; } else if (*b=='e'||*b=='E') { t=14*k; s=s+t; } else if (*b=='f'||*b=='F') { t=15*k; s=s+t; } else if (*b=='0') { t=0; s=s+t; } else { printf(" ERROR"); getch(); exit(0); } } printf(" Its decimal equivalent is %d.",s); return; }