/************************************************************
Program to convert a decimal number to a hexadecimal number
************************************************************/
#include<stdio.h> // include stdio.h library
int main(void)
{
int num, bin = 0;
int i = 0, rem;
char hex_arr[50];
printf("Enter a decimal number: \n");
scanf("%d", &num);
while(num != 0)
{
rem = num % 16; // get the right most digit
if (rem < 10)
{
hex_arr[i++] = 48 + rem;
}
else
{
hex_arr[i++] = 55 + rem;
}
num /= 16; // get the quotient
}
printf("0x");
for(int j = i - 1; j >= 0 ; j--) // print the hex_arr in reverse order
{
printf("%c", hex_arr[j]);
}
return 0; // return 0 to operating system
}