C Programming
Computer Programming

C Program using function to Convert Hexadecimal to Decimal



Write a Program in C using function to Convert a Hexadecimal to Decimal using function

\* C Program using function to Convert Hexadecimal to Decimal *\

# include < stdio.h >
# include < conio.h >
# include < math.h >
int   HexadecimalToDecimal( char *hex )
{

int p = 0 ;
int decimal = 0 ;
int r, i ;
for( i = strlen(hex) - 1 ; i >= 0 ; --i )
{
      if(hex[i]>='0'&&hex[i]<='9')
      {
            r = hex[i] - '0' ;
      }
      else
      {
            r = hex[i] - 'A' + 10 ;
      }
      decimal = decimal + r * pow(16 , p ) ;
      ++p ;
}
return decimal ;

}

int   main( )
{

char hex[100] ;
printf( "\n Enter Hexadecimal Number ( 0 - 9 and A - F ): " ) ;
scanf( "%s", hex) ;
printf( "\n Decimal are : %d", HexadecimalToDecimal(hex) ) ;
return 0 ;

}

Output of Program :

Output of C Program using function to Convert Hexadecimal to Decimal