-
Notifications
You must be signed in to change notification settings - Fork 0
/
08-14.c
61 lines (41 loc) · 1.24 KB
/
08-14.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// conversion of positive integer of base ten to another base, 07-07 modified
#include <stdio.h>
int convertedNumber[64], base, digit = 0;
long int numberToConvert;
void getNumberAndBase(void) { // input number and base
printf("number to convert: ");
scanf("%ld", &numberToConvert);
printf("base: ");
scanf("%i", &base);
if (base < 2 || base > 16) {
printf("base must be between 2 and 16\n");
base = 10;
}
}
void convertNumber(void) { // convert to the input base
do {
convertedNumber[digit] = numberToConvert % base;
digit++;
numberToConvert /= base;
} while (numberToConvert);
}
void displayConvertedNumber(void) { // display converted number
const char baseDigits[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
int nextDigit;
printf("converted number: ");
for (digit--; digit >= 0; digit--) {
nextDigit = convertedNumber[digit];
printf("%c", baseDigits[nextDigit]);
}
printf("\n");
}
int main(void) {
void getNumberAndBase(void), convertNumber(void), displayConvertedNumber(void);
getNumberAndBase();
convertNumber();
displayConvertedNumber();
return 0;
}