C program to convert hexadecimal into decimal.

 

01 #include <stdio.h>
02 #include <math.h>
03 #include <string.h>
04
05 int main()
06 {
07 char hex[17];
08
09 long int decimal=0;
10
11 int i = 0, val, len;
12
13 printf("Enter any hexadecimal number: ");
14 gets(hex);
15
16 /* length of hex digit */
17
18 len = strlen(hex);
19 len--;
20
21 for(i=0; hex[i]!=''; i++)
22 {
23 switch(hex[i])
24 {
25 case '0':
26 val = 0;
27 break;
28 case '1':
29 val = 1;
30 break;
31 case '2':
32 val = 2;
33 break;
34 case '3':
35 val = 3;
36 break;
37 case '4':
38 val = 4;
39 break;
40 case '5':
41 val = 5;
42 break;
43 case '6':
44 val = 6;
45 break;
46 case '7':
47 val = 7;
48 break;
49 case '8':
50 val = 8;
51 break;
52 case '9':
53 val = 9;
54 break;
55 case 'a':
56 case 'A':
57 val = 10;
58 break;
59 case 'b':
60 case 'B':
61 val = 11;
62 break;
63 case 'c':
64 case 'C':
65 val = 12;
66 break;
67 case 'd':
68 case 'D':
69 val = 13;
70 break;
71 case 'e':
72 case 'E':
73 val = 14;
74 break;
75 case 'f':
76 case 'F':
77 val = 15;
78 break;
79 }
80
81 decimal += val * pow(16, len);
82 len--;
83 }
84
85 printf("Decimal number = %ld",decimal);
86 return 0;
87 }
 
 OUTPUT :

Enter any hexadecimal number : 1E
Decimal number = 30

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.