Steps to convert Decimal number into Hexadecimal number -
- Divide the decimal number by 16. Treat the division as an integer division.
- Write down the remainder (in hexadecimal, ie. if the remainder is 12, write down "C").
- Divide the result again by 16. Treat the division as an integer division.
- Repeat step 2 and 3 until result is less than 1.
- The hexadecimal value is the digit sequence of the remainders from the last to first. (So if you had 001, it's 100.)
Sample Output -
Code -
/* Program to convert Decimal number into Hexadecimal number */
#include<stdio.h>
#include<conio.h>
int main()
{
int decimal_number = 0, temp_number = 0, remainder = 0, counter = 0, i = 0, value = 0;
char hexadecimal_number[10];
printf("\n\t __ Program to convert Decimal number into Hexadecimal number __");
printf("\n\n\n Enter the Decimal Number - ");
scanf("%d",&decimal_number);
temp_number = decimal_number;
while(temp_number > 1)
{
remainder = temp_number % 16;
temp_number = temp_number / 16;
value = 0;
if (remainder >= 0 && remainder <= 9)
{
value = 48;
for ( i = 1; i <= remainder; i ++)
{
value = value + 1;
}
}
else if (remainder >= 10 && remainder <= 15)
{
value = 65;
for ( i = 11; i <= remainder; i ++)
{
value = value + 1;
}
}
hexadecimal_number[counter] = (char)value;
counter++;
}
printf("\n\n Hexadecimal Conversion of %d (base 10) is ", decimal_number);
for( i = counter - 1 ; i >= 0; i -- )
{
printf("%c", hexadecimal_number[i]);
}
printf(" (base 16)");
getch();
return 0;
}
No comments:
Post a Comment