(Recursive Exponentiation) Write a recursive function power( base, exponent ) that, when invoked, returns base exponent For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. Hint: The recursion step would use the relationship base exponent = base ยท base exponent - 1 and the terminating condition occurs when exponent is equal to 1, because base1 = base
What will be an ideal response?
```
// Recursive exponentiation.
#include
using namespace std;
long power( long, long ); // function prototype
int main()
{
long b; // base
long e; // exponent
cout << "Enter a base and an exponent: ";
cin >> b >> e;
// calculate and display b^e
cout << b << " raised to the " << e << " is " << power( b, e ) << endl;
} // end main
// power recursively calculates base^exponent, assume exponent >= 1
long power( long base, long exponent )
{
if ( exponent == 1 ) // base case: exponent equals 1, return base
return base;
else // recursion step
return base * power( base, exponent - 1 );
} // end function power
```
You might also like to view...
The order of precedence in Excel is the same as the basic order of operations in math
Indicate whether the statement is true or false
The main purpose of a site is to create users and groups for a particular local area network
Indicate whether the statement is true or false