program to convert hexadecimal to decimal - C-Tutorial

Latest

Sunday, 8 December 2019

program to convert hexadecimal to decimal


/* program to convert number from hexadecimal to decimal*/

#include <stdio.h>
#include <string.h>
#include <math.h>

void main()
{
    char hex[32]={0};
    int  dec = 0, i, cnt = 0, dig;
    clrscr();
    printf("Enter hexa decimal value: ");
    gets(hex);
    for(i=(strlen(hex)-1);i>=0;i--)
    {
        switch(hex[i])
        {
            case 'A':
                dig=10; break;
            case 'B':
                dig=11; break;
            case 'C':
                dig=12; break;
            case 'D':
                dig=13; break;
            case 'E':
                dig=14; break;
            case 'F':
                dig=15; break;
            default:
                dig=hex[i]-48;
        }
        dec= dec + (dig)*pow((double)16,(double)cnt);
        cnt++;
    }
    printf("DECIMAL value is: %d", dec);
    getch();
}

No comments:

Post a Comment