|
Get 9,000+ Interview Questions with Answers in an eBook
C Source Codes List | Source Code Home
Program : Program to display the decimal equivalent of an i/p hexadecimal number.
/* Prg. to display the decimal equivalent of an i/p hexadecimal number */
#include
#include
#include
#include
void htod(char b[]);
int c;
void main()
{
char a[3];
int j;
clrscr();
printf("How many digits in input? ");
scanf("%d",&c);
printf("\nDigits of your i/p no. must not exceed entered limit or 3(max).");
printf("\nEnter a hexadecimal no.");
printf("\nNo. will be accepted if you press ENTER after inputting no.");
printf("\nNUMBER:- ");
scanf("%s",a);
htod (a);
getch();
}
void htod(char b[])
{
int i,e,n,k=0,t=0,s=0;
n=*b;
for (i=c-1;i>=0;--i)
{ e =(c-1)-i;
if (i>0)
*b=*(b+i);
else
*b=n;
k = pow(16,e);
if (*b =='1')
{
t=1*k;
s=s+t;
}
else if (*b=='2')
{
t=2*k;
s=s+t;
}
else if (*b=='3')
{
t=3*k;
s=s+t;
}
else if (*b=='4')
{
t=4*k;
s=s+t;
}
else if (*b=='5')
{
t=5*k;
s=s+t;
}
else if (*b=='6')
{
t=6*k;
s=s+t;
}
else if (*b=='7')
{
t=7*k;
s=s+t;
}
else if (*b=='8')
{
t=8*k;
s=s+t;
}
else if (*b=='9')
{
t=9*k;
s=s+t;
}
else if (*b=='a'||*b=='A')
{
t=10*k;
s=s+t;
}
else if (*b=='b'||*b=='B')
{
t=11*k;
s=s+t;
}
else if (*b=='c'||*b=='C')
{
t=12*k;
s=s+t;
}
else if (*b=='d'||*b=='D')
{
t=13*k;
s=s+t;
}
else if (*b=='e'||*b=='E')
{
t=14*k;
s=s+t;
}
else if (*b=='f'||*b=='F')
{
t=15*k;
s=s+t;
}
else if (*b=='0')
{
t=0;
s=s+t;
}
else
{
printf("\nERROR");
getch();
exit(0);
}
}
printf("\nIts decimal equivalent is %d.",s);
return;
}
C Source Codes List | Source Code Home
|
|
|