programming-examples/c/Basic/C program to Convert Decimal to Hexadecimal.c

27 lines
702 B
C
Raw Normal View History

2019-11-18 14:44:36 +01:00
/*
* C program to Convert Decimal to Hexadecimal
*/
#include <stdio.h>
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;
}