Example
Input
Input decimal number: 26
Output
Hexadecimal number: 1A
Algorithm to convert decimal to hexadecimal number system
Algorithm Conversion from Decimal to Hexadecimal begin: read (decimal); hex ← NULL; rem ← 0; HEXVALUES[] ← 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F; While(decimal != 0) begin: rem ← decimal % 16; hex ← hex + HEXVALUES[rem]; decimal ← decimal / 16; end; Reverse(hex); print('Hexadecimal = ' hex); end;
/**
* C program to convert from Decimal number system to hexadecimal number system
*/
#include <stdio.h>
#include <string.h>
int main()
{
char HEXVALUE[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
long long decimal, tempDecimal;
char hex[65];
int index, rem;
/* Input decimal number from user */
printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;
index = 0;
/* Decimal to hexadecimal conversion */
while(tempDecimal !=0)
{
rem = tempDecimal % 16;
hex[index] = HEXVALUE[rem];
tempDecimal /= 16;
index++;
}
hex[index] = '\0';
strrev(hex);
printf("\nDecimal number = %lld\n", decimal);
printf("Hexadecimal number = %s", hex);
return 0;
}
/*
Output :
Enter any decimal number: 427
Decimal number = 427
Hexadecimal number = 1AB
*/