-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-07.c
46 lines (35 loc) · 1.21 KB
/
07-07.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// conversion of positive integer of base ten to another base
/* some weak spots:
1. there is no check if the base is indeed between 2 and 16
2. if the input for the base is 0, the division on line 27 gives an error
3. if the input is 1 the conversion loop runs infinitely
4. or, if it is more than 16 the index runs out of the limits of the array
(there is a version in chapter 8 with the solutions) */
#include <stdio.h>
int main(void) {
const char baseDigits[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
int convertedNumber[64], nextDigit, base, index = 0;
long int numberToConvert;
// input number and base
printf("number to convert: ");
scanf("%ld", &numberToConvert);
printf("base: ");
scanf("%i", &base);
// convert to the input base
do {
convertedNumber[index] = numberToConvert % base;
index++;
numberToConvert /= base;
} while (numberToConvert);
// display converted number
printf("converted number: ");
for (index--; index >= 0; index--) {
nextDigit = convertedNumber[index];
printf("%c", baseDigits[nextDigit]);
}
printf("\n");
return 0;
}