Q. Write a program in C to convert a decimal number to its hexadecimal equivalent.
Ans.
/*c program for convert decimal value to hexadecimal value*/
#include<stdio.h>
#include<conio.h>
void decTohex(int num);
int main()
{
int n;
printf("Enter number : ");
scanf("%d",&n);
decTohex(n);
getch();
getch();
return 0;
}
void decTohex(int num)
{
int i=0,j=0,rem[5];
for( ; num>15; num=num/16)
{
rem[i]=num%16;
i++;
j++;
}
rem[i]=num;
for(i=j; i>=0; --i)
{
if(rem[i]==10)
printf("A");
else if(rem[i]==11)
printf("B");
else if(rem[i]==12)
printf("C");
else if(rem[i]==13)
printf("D");
else if(rem[i]==14)
printf("E");
else if(rem[i]==15)
printf("F");
else
printf("%d",rem[i]);
}
}
/*****************OUTPUT****************
Enter number : 1785
6F9
}
void decTohex(int num)
{
int i=0,j=0,rem[5];
for( ; num>15; num=num/16)
{
rem[i]=num%16;
i++;
j++;
}
rem[i]=num;
for(i=j; i>=0; --i)
{
if(rem[i]==10)
printf("A");
else if(rem[i]==11)
printf("B");
else if(rem[i]==12)
printf("C");
else if(rem[i]==13)
printf("D");
else if(rem[i]==14)
printf("E");
else if(rem[i]==15)
printf("F");
else
printf("%d",rem[i]);
}
}
/*****************OUTPUT****************
Enter number : 1785
6F9
**************************************/
Comments
Post a Comment