From 39c0cbcc70aff47e17107826aa76cb525c83b122 Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 9 Jul 2019 16:00:48 -0400 Subject: ch2 --- ch1/power.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 ch1/power.c (limited to 'ch1/power.c') diff --git a/ch1/power.c b/ch1/power.c new file mode 100644 index 0000000..8bca4fd --- /dev/null +++ b/ch1/power.c @@ -0,0 +1,28 @@ +#include + +int power(int m, int n); + +/* + * Test power function + */ +main() { + int i; + + for (i = 0; i < 10; ++i) + printf("%d %d %d\n", i, power(2, i), power(-3, i)); + + return 0; +} + +/* + * Raise base to n-th power; n >= 0 + */ +int power(int base, int n) { + int i, p; + + p = 1; + + for (i = 1; i <= n; ++i) + p = p * base; + return p; +} -- cgit v1.2.3