Write a C program to read hexadecimal number from user and convert it to Decimal number system. How to convert from Hexadecimal number system to Decimal number system in C.

Input hexadecimal number: (1A)

Output decimal number: (26)

Step 1: Read Hexadecimal number.

Step 2: Set decimal = 0, digit = length_of_hexadecimal_digit - 1 and i = 0.

Step 3: decimal = decimal + (hex[i] * 16 ^ digit).

Step 4: digit = digit - 1.

Step 5: Increment the value of i i.e. i = i + 1.

Step 6: Repeat Step 3-5 till the end of hex digit.

Happy coding ;)

**Example:**Input hexadecimal number: (1A)

_{16}Output decimal number: (26)

_{10}### Required knowledge:

Basic C programming, For loop#### Hexadecimal number system

Hexadecimal number system is a base 16 number system. It uses 16 symbols to represent all number i.e. 0 1 2 3 4 5 6 7 8 9 A B C D E F#### Decimal number system

Decimal number system is a base 10 number system. It uses 10 symbols to represent all numbers i.e 0 1 2 3 4 5 6 7 8 9### Algorithm to convert Hexadecimal to Decimal

Steps to convert from Hexadecimal number system to decimal number system:Step 1: Read Hexadecimal number.

Step 2: Set decimal = 0, digit = length_of_hexadecimal_digit - 1 and i = 0.

Step 3: decimal = decimal + (hex[i] * 16 ^ digit).

Step 4: digit = digit - 1.

Step 5: Increment the value of i i.e. i = i + 1.

Step 6: Repeat Step 3-5 till the end of hex digit.

**Hexadecimal to decimal conversion table**Hexadecimal | Decimal |

0 | 0 |

1 | 1 |

2 | 2 |

3 | 3 |

4 | 4 |

5 | 5 |

6 | 6 |

7 | 7 |

8 | 8 |

9 | 9 |

A | 10 |

B | 11 |

C | 12 |

D | 13 |

E | 14 |

F | 15 |

### Program:

/** * C program to convert Hexadecimal to Decimal number system */ #include <stdio.h> #include <math.h> #include <string.h> int main() { char hex[17]; long long decimal, place; int i = 0, val, len; decimal = 0; place = 1; /* * Reads hexadecimal number from user */ printf("Enter any hexadecimal number: "); gets(hex); /* Finds the length of total number of hex digit */ len = strlen(hex); len--; /* * Converts the hexadecimal number to decimal number * using algorithm decimal = decimal + (hex[i] * 16 ^ digit) */ for(i=0; hex[i]!='\0'; i++) { /* * Finds the decimal each equivalent hexadecimal digit */ switch(hex[i]) { case '0': val = 0; break; case '1': val = 1; break; case '2': val = 2; break; case '3': val = 3; break; case '4': val = 4; break; case '5': val = 5; break; case '6': val = 6; break; case '7': val = 7; break; case '8': val = 8; break; case '9': val = 9; break; case 'a': case 'A': val = 10; break; case 'b': case 'B': val = 11; break; case 'c': case 'C': val = 12; break; case 'd': case 'D': val = 13; break; case 'e': case 'E': val = 14; break; case 'f': case 'F': val = 15; break; } decimal += val * pow(16, len); len--; } printf("\nHexadecimal number = %s\n", hex); printf("Decimal number = %lld", decimal); return 0; }

Output

Enter any hexadecimal number: 1a

Hexadecimal number = 1a

Decimal number = 26

Hexadecimal number = 1a

Decimal number = 26

Happy coding ;)

### You may also like

- Loop programming exercises and solutions in C.
- C program to find one's complement of a binary number.
- C program to find two's complement of a binary number.
- C program to convert Binary to Octal number system.
- C program to convert Binary to Decimal number system.
- C program to convert Binary to Hexadecimal number system.
- C program to convert Octal to Binary number system.
- C program to convert Octal to Decimal number system.
- C program to convert Octal to Hexadecimal number system.
- C program to convert Decimal to Binary number system.
- C program to convert Decimal to Octal number system.
- C program to convert Decimal to Hexadecimal number system.
- C program to convert Hexadecimal to Binary number system.
- C program to convert Hexadecimal to Octal number system.

< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).