// ex_ch9_7.c -- power.c, but also of negative exponents
#include <stdio.h>
//#include <inttypes.h>

// ANSI prototype
double power(double n, int p);

int main(void)
{
	double x, xpow;
	int exp;

	printf("Enter a number and the integer power to which\n"
		"the number will be rised. Enter q to quit.\n");

	while(scanf("%lf%d", &x, &exp) == 2)
	{
		// function call
		xpow = power(x, exp);
		printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
		printf("Enter next pair of numbers or q to quit.\n");
	}
	printf("Hope you enjoyed this power trip -- bye!\n");
	return 0;
}

// function definition
double power(double n, int p)
{
	double pow = 1;
	int i;

	if (p > 0)
	{
		for (i = 1; i <= p; i++)
			pow *= n;
	}
	else if (p < 0)
	{
		p = abs(p);
		for (i = 1; i <= p; i++)
			pow *= n;
		pow = 1/pow;
	}
	// return the value of pow
	return pow;
}
