C Program To Convert Hexadecimal To Decimal

C Program To Convert Hexadecimal To Decimal: In this program, we will use nested for loop to convert hexadecimal to decimal.

C Program To Convert Hexadecimal To Decimal

#include <stdio.h>
#include <math.h>
int main()
{
  char hv[10];
  int len = 0, sum = 0, i, j, p = 0;
  char hexDig[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  printf("Enter the input: ");
  scanf("%s", hv);
  for (i = 0; hv[i] != '\0'; i++)
    len++;
  for (i = len - 1; i >= 0; i--)
  {
    for (j = 0; j < 16; j++)
    {
      if (hv[i] == hexDig[j])
      {
        sum = sum + j * pow(16, p);
      }
    }
    p++;
  }
  printf("%d", sum);
  return 0;
}

Output

Enter the input: A
10
Enter the input: A1
161

Read: C Program To Convert Decimal To Binary


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment