// FILE: powers.cxx // A demonstration program for two recursive functions that compute powers. #include // Provides assert #include // Provides EXIT_SUCCESS #include // Provides cin, cout using namespace std; double power(double x, int n); double pow(double x, int n); int main( ) { double x; int n; cout << "Please type a value for x (double) and n (int) : "; cin >> x >> n; cout << "The value of x to the n is: " << power(x, n) << endl; cout << "Please type a value for x (double) and n (int) : "; cin >> x >> n; cout << "The value of x to the n is: " << power(x, n) << endl; return EXIT_SUCCESS; } double power(double x, int n) { double product; // The product of x with itself n times int count; if (x == 0) assert(n > 0); if (n >= 0) { product = 1; for (count = 1; count <= n; count++) product = product * x; return product; } else return 1/power(x, -n); } double pow(double x, int n) { if (x == 0) { assert(n > 0); return 0; } else if (n == 0) return 1; else if (n > 0) return x * pow(x, n-1); else // x is nonzero, and n is negative return 1/pow(x, -n); }