C Programming Code Examples C > For Loops and While Loops Code Examples C program to convert Hexadecimal to Decimal number system C program to convert Hexadecimal to Decimal number system Write a C program to input hexadecimal number from user and convert it to Decimal number system. How to convert from Hexadecimal number system to Decimal number system in C programming. Hexadecimal number system Hexadecimal number system is a base 16 number system. It uses 16 symbols to represent all number i.e. 0123456789ABCDEF Decimal number system Decimal number system is a base 10 number system. It uses 10 symbols to represent all numbers i.e. 0123456789 Input a hexadecimal number from user. Store it in some variable hex. Initialize decimal = 0, digit = length_of_hexadecimal_digit - 1 and j = 0. Run a loop for each hex digit. Which is the loop structure should look like for(j=0; hex[j]!='\0'; j++). Inside the loop find the integer value of hex[j]. Store it in some variable say val. Convert the hex to decimal using decimal = decimal + (val * 16 ^ digit). Where val = hex[j]. #include <stdio.h> #include <math.h> #include <string.h> int main() { char hex[17]; long long decimal, place; int j = 0, val, len; decimal = 0; place = 1; /* Input hexadecimal number from user */ printf("Enter any hexadecimal number: "); gets(hex); /* Find the length of total number of hex digit */ len = strlen(hex); len--; /* Iterate over each hex digit */ for(j=0; hex[j]!='\0'; j++) { /* Find the decimal representation of hex[j] */ if(hex[j]>='0' && hex[j]<='9') { val = hex[j] - 48; } else if(hex[j]>='a' && hex[j]<='f') { val = hex[j] - 97 + 10; } else if(hex[j]>='A' && hex[j]<='F') { val = hex[j] - 65 + 10; } decimal += val * pow(16, len); len--; } printf("Hexadecimal number = %s\n", hex); printf("Decimal number = %lld", decimal); return 0; }