This is a C program to Convert Decimal to Hexadecimal.
Problem Description
This program takes a decimal number as input and converts to hexadecimal.
Problem Solution
1. Take a decimal number as input.
2. Divide the input number by 16. Store the remainder in the array.
3. Do step 2 with the quotient obtained until quotient becomes zero.
4. Print the array in the reversed fashion to get hexadecimal number.
Program/Source Code
Here is source code of the C program to Convert Decimal to Hexadecimal. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C program to Convert Decimal to Hexadecimal
*/
#include
int main()
{
long decimalnum, quotient, remainder;
int i, j = 0;
char hexadecimalnum[100];
printf(“Enter decimal number: “);
scanf(“%ld”, &decimalnum);
quotient = decimalnum;
while (quotient != 0)
{
remainder = quotient % 16;
if (remainder < 10)
hexadecimalnum[j++] = 48 + remainder;
else
hexadecimalnum[j++] = 55 + remainder;
quotient = quotient / 16;
}
// display integer into character
for (i = j; i >= 0; i–)
printf(“%c”, hexadecimalnum[i]);
return 0;
}
Runtime Test Cases
Output:
Enter decimal number: 12
Equivalent hexadecimal value of 12 : C