C Program To Convert Hexadecimal To Decimal Number
Let us learn how to convert hexadecimal to decimal number in C programming language. This C code for hexadecimal number to decimal conversion makes use of pow() function, while loop and for loop.
Hexadecimal Number
A hexadecimal number has a base 16 and includes the following values: 0 1 2 3 4 5 6 7 8 9 A B C D E F where,
A = 10, B = 11, C = 12, D = 13, E = 14, F = 15.
Decimal Number
A decimal number has a base 10 and includes digits the following digits: 0 1 2 3 4 5 6 7 8 9
Method 1: C Program To Convert Hexadecimal To Decimal Value using While Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include<stdio.h> #include<math.h> int main() { int decimal_number = 0, remainder, hexadecimal_number; int count = 0; printf("Enter a Hexadecimal Number:\t"); scanf("%d", &hexadecimal_number); while(hexadecimal_number > 0) { remainder = hexadecimal_number % 10; decimal_number = decimal_number + remainder * pow(16, count); hexadecimal_number = hexadecimal_number / 10; count++; } printf("\nDecimal Equivalent:\t%d\n", decimal_number); return 0; } |
Method 2: C Program For Hexadecimal Number To Decimal Conversion using For Loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include<stdio.h> #include<math.h> int main() { int decimal_number = 0, remainder, hexadecimal_number; int count; printf("Enter a Hexadecimal Number:\t"); scanf("%d", &hexadecimal_number); for(count = 0; hexadecimal_number > 0; count++) { remainder = hexadecimal_number % 10; decimal_number = decimal_number + remainder * pow(16, count); hexadecimal_number = hexadecimal_number / 10; } printf("\nDecimal Equivalent:\t%d\n", decimal_number); return 0; } |
Method 3: C Program To Convert Hexadecimal Number To Decimal Number using Functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<stdio.h> #include<math.h> int hexadecimal_to_decimal(int x) { int decimal_number, remainder, count = 0; while(x > 0) { remainder = x % 10; decimal_number = decimal_number + remainder * pow(16, count); x = x / 10; count++; } return decimal_number; } int main() { int hexadecimal_number, result; printf("Enter a Hexadecimal Number:\t"); scanf("%d", &hexadecimal_number); result = hexadecimal_to_decimal(hexadecimal_number); printf("\nDecimal Equivalent:\t%d\n", result); return 0; } |
Output

If you have any compilation error or doubts in this C program for hexadecimal value to decimal number conversion, let us know about it in the comment section below.
The pow() is not working. When I compile this program, it gives me an error.
While compiling the code, use this command: gcc testfile.c -lm
This was really amazing. So many implementation methods for converting hexadecimal number to decimal numbers in C. Made my concepts more clear.
this cod ewill not work if the input is for eg;- 1A, 12AD etc. Because these are also hexa decimal number .
THanks so much. This Hex to Dec number conversion is so much easy.
what about 1A?
hexadecimal means it mainly involves A,B,C,D,E .you didnt show any code to convert that
what if i take input like 1A,6D etc then its not working
exactly its not working invokving 1A,4E…