Decimal number system is a base 10 number system using digits 0 and 9 whereas Hexadecimal number system is base 16 number system and using digits from 0 to 9 and A to F. Given an decimal number as input from user convert it to hexadecimal number.
For Example:
1234 in Decimal is equivalent to 4D2 in Hexadecimal number system.
Required Knowledge
C program to convert a decimal number to hexadecimal number
#include <stdio.h> #include <string.h> #define BASE_16 16 int main() { char hexDigits[] = "0123456789ABCDEF"; long decimalNumber; char hexadecimalNumber[40]; int index, remaindar; /* * Take a Decimal Number as input form user */ printf("Enter a Decimal Number\n"); scanf("%ld", &decimalNumber); index = 0; /* Convert Decimal Number to Hexadecimal Numbers */ while(decimalNumber != 0) { remaindar = decimalNumber % BASE_16; hexadecimalNumber[index] = hexDigits[remaindar]; decimalNumber /= BASE_16; index++; } hexadecimalNumber[index] = '\0'; strrev(hexadecimalNumber); printf("Hexadecimal Number : %s", hexadecimalNumber); return 0; }Output
Enter a Decimal Number 1234 Hexadecimal Number : 4D2
Enter a Decimal Number 1000 Hexadecimal Number : 3E8
Related Topics