// Program: power.cpp, an example recursive program // Authors: Dr Keith Shomper // Date: 10/30/2008 #include #include using namespace std; int myPower(int, int); int main() { int p, b, e; cout << "This program computes expontials of integer bases to non-negative integer powers\n"; cout << "\tWhat is your base? "; cin >> b; cout << "\tWhat is your exponent? "; cin >> e; // evidently the user doesn't understand the instructions, so exit if (e < 0) { cout << "Please read the instructions for allowable inputs\n"; return -1; } //compute b to the e power using the standard pow() function p = (int) pow(b, e); cout << b << " raised to the " << e << " power is " << p << endl; //compute b to the e power using the standard pow() function p = myPower(b, e); cout << b << " raised to the " << e << " power is " << p << endl; return 0; } int myPower(int b, int e) { if (e == 0) { return 1; } else { return b * myPower(b, e-1); } }