Hi, i'm a newbie to C and currently trying to learn the language, so at the moment i only really know the basic stuff. I'm trying to create a program that converts a decimal number (entered by the user) into hexidecimal. I have got it working for one single, decimal digit, but i have hit a mental block and can't seem to see how i am able to do it for when the decimal number is above 15. Here is what i have so far:
#include <stdio.h>
char hex;
unsigned int decimal; //stores decimal number that needs to be converted.
int main()
{
printf("Please enter the digit: ");
scanf("%u", &decimal);
if (decimal < 10)
hex = '0' + decimal;
else hex = 'A' + (decimal - 10);
printf("%c ",hex);
return 0;
}
If you enter 16 for example, it displays G (as i expected) but obviously it needs to loop round some how so that it displays 10. I'm thinking it would be a FOR loop but again, i can't see how it would work.
Any help would be greatly appreciated! (Please try and keep it as simple as possible, some of the posts on here make my mind blow up. )