...

/

Solution Review: Calculate the Power of a Number Recursively

Solution Review: Calculate the Power of a Number Recursively

Let's go over the solution review of the challenge given in the previous lesson.

We'll cover the following...

Solution

Press the RUN button and see the output!

Press + to interact
#include <iostream>
using namespace std;
// Recursive power function
int power(int base, int exponent) {
// Base case
if (exponent == 0) {
return 1;
}
// Recursive case
else
return base * power(base, exponent - 1);
}
// main function
int main() {
// Initialize base and exponent
int base = 2, exponent = 4;
// Declare variable result
int result;
// Call power in main and store the returned value in result
result = power(base, exponent);
// Print value of result
cout << base << " raised to power " << exponent << " = " << result;
return 0;
}

Explanation

power function

The recursive power function takes two values of type int in its ...