# Program to convert Hexadecimal to Decimal

Problem statement:-  Program to convert Hexadecimal to Decimal.

## Data requirement:-    Input Data:- sizea, hex   Output Data:- decimal   Additional Data:-i, j, value Program in C

Here is the source code of the C program to convert Hexadecimal number to Decimal number.

Code:

#include<stdio.h>
#include<string.h>
#include<math.h>
int main ()
{
int sizea,i;
printf ("Enter the array size:");
scanf ("%d", &sizea);
char hex[sizea+1];
for (i = 0; i < sizea+1; i++)
{
scanf ("%c", &hex[i]);
}

int value=0;
int decimal=0;
int j=strlen(hex);
j--;
for(i=0;hex[i]!='\0';i++)
{
switch (hex[i])
{
case '0':
value=0;
break;
case '1':
value=1;
break;
case '2':
value=2;
break;
case '3':
value=3;
break;
case '4':
value=4;
break;
case '5':
value=5;
break;
case '6':
value=6;
break;
case '7':
value=7;
break;
case '8':
value=8;
break;
case '9':
value=9;
break;
case 'A':
case 'a':
value=10;
break;
case 'B':
case 'b':
value=11;
break;
case 'C':
case 'c':
value=12;
break;
case 'D':
case 'd':
value=13;
break;
case 'E':
case 'e':
value=14;
break;
case 'F':
case 'f':
value=15;
break;
}
decimal+=value*pow(16,j);
j--;
}
printf("Decimal Number is: %d",decimal);
}

Input/Output:
Enter the array size:2
Decimal Number is: 238

Program in C++

Here is the source code of the C++ program to convert Hexadecimal to a decimal using a while loop.

Code:

#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main ()
{
int sizea,i;
cout<<"Enter the array size:";
cin>>sizea;
char hex[sizea];
for (i = 0; i < sizea; i++)
{
cin>>hex[i];
}

int value=0;
int decimal=0;
int j=sizea;
j--;
for(i=0;hex[i]!='\0';i++)
{
switch (hex[i])
{
case '0':
value=0;
break;
case '1':
value=1;
break;
case '2':
value=2;
break;
case '3':
value=3;
break;
case '4':
value=4;
break;
case '5':
value=5;
break;
case '6':
value=6;
break;
case '7':
value=7;
break;
case '8':
value=8;
break;
case '9':
value=9;
break;
case 'A':
case 'a':
value=10;
break;
case 'B':
case 'b':
value=11;
break;
case 'C':
case 'c':
value=12;
break;
case 'D':
case 'd':
value=13;
break;
case 'E':
case 'e':
value=14;
break;
case 'F':
case 'f':
value=15;
break;
}
decimal+=value*pow(16,j);
j--;
}
cout<<"Decimal Number is: "<<decimal;
}

Input/Output:
Enter the array size:2
Octal Number is: 237

Program in Java

Here is the source code of the Java program to convert a hexadecimal number to a decimal number.

Code:

import java.util.Scanner;

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int i;
String hex;
hex=cs.nextLine();
int value=0;
int decimal=0;
int j=hex.length();
j--;
for(i=0;i<hex.length();i++)
{
switch (hex.charAt(i))
{
case '0':
value=0;
break;
case '1':
value=1;
break;
case '2':
value=2;
break;
case '3':
value=3;
break;
case '4':
value=4;
break;
case '5':
value=5;
break;
case '6':
value=6;
break;
case '7':
value=7;
break;
case '8':
value=8;
break;
case '9':
value=9;
break;
case 'A':
case 'a':
value=10;
break;
case 'B':
case 'b':
value=11;
break;
case 'C':
case 'c':
value=12;
break;
case 'D':
case 'd':
value=13;
break;
case 'E':
case 'e':
value=14;
break;
case 'F':
case 'f':
value=15;
break;
}
decimal+=value*Math.pow(16,j);
j--;
}
System.out.println("Decimal Number is: "+decimal);
cs.close();
}
}

Input/Output:
EFA
Decimal Number is: 3834

Program in Python

Here is the source code of the Python Program program to convert hexadecimal to decimal without function.

Code:

import math
value=0
decimal=0
j=len(hex)
j-=1
for i in range(0,len(hex)):
if hex[i]>='0' and hex[i]<='9' :
value=(int)(hex[i])
if hex[i]=='A' or hex[i]=='a':
value=10
if hex[i] == 'B' or hex[i] == 'b':
value=11
if hex[i] == 'C' or hex[i] == 'c':
value=12
if hex[i] == 'D' or hex[i] == 'd':
value=13
if hex[i] == 'E' or hex[i] == 'e':
value=14
if hex[i] == 'F' or hex[i] == 'f':
value=15
decimal=decimal+(int)(value*math.pow(16,j))
j-=1
print("Decimal Number is:",decimal)

Input/Output: