-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise10.c
55 lines (42 loc) · 1.17 KB
/
exercise10.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
// C Primer Plus
// Chapter 9 Exercise 10:
// Generalize the to_binary() function of Listing 9.8 to a to_base_n() function
// that takes a second argument in the range 2–10. It then should print the
// number that is its first argument to the number base given by the second
// argument. For example, to_ base_n(129,8) would display 201, the base-8
// equivalent of 129. Test the function in a complete program.
#include <stdio.h>
#include <stdlib.h>
void to_base_n(int integer, int base);
int main(void) {
int integer, base;
printf("Test to_base_n() function\n");
printf("Enter an integer in base 10 and a base to convert to: ");
while (scanf("%d %d", &integer, &base) == 2)
{
to_base_n(integer, base);
putchar('\n');
printf("Enter an integer in base 10 and a base to convert to: ");
}
return 0;
}
void to_base_n(int integer, int base)
{
// handle invalid bases
if (base < 2 || 10 < base)
{
printf("Error: base must be between 2 and 10.");
return;
}
// stop recursion
if (integer == 0) return;
// handle negative integers
if (integer < 0)
{
putchar('-');
integer = abs(integer);
}
to_base_n(integer / base, base);
printf("%d", integer % base);
return;
}