Created
September 8, 2016 04:30
-
-
Save twof/f66779b16f50de7f537f1b55303380e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(void) | |
{ | |
char base_digits[16] = | |
{'0', '1', '2', '3', '4', '5', '6', '7', | |
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; | |
int convertedNumber; | |
int* ptrToConvertedNumber = &convertedNumber; | |
long int numberToConvert; | |
int base, index = 0; | |
/* get the number and base */ | |
printf("Enter number and desired base: "); | |
scanf("%ld %i", &numberToConvert, &base); | |
/* convert to the indicated base */ | |
while (numberToConvert != 0) | |
{ | |
*(ptrToConvertedNumber + index) = numberToConvert % base; | |
numberToConvert = numberToConvert / base; | |
++index; | |
} | |
/* now print the result in reverse order */ | |
--index; /* back up to last entry in the array */ | |
printf("\n\nConverted Number = "); | |
for( ; index>=0; index--) /* go backward through array */ | |
{ | |
printf("%c", base_digits[*(ptrToConvertedNumber+index)]); | |
} | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment