...
/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...
We'll cover the following...
Solution
Press the RUN button and see the output!
Press + to interact
C++
#include <iostream>using namespace std;// Recursive power functionint power(int base, int exponent) {// Base caseif (exponent == 0) {return 1;}// Recursive caseelsereturn base * power(base, exponent - 1);}// main functionint main() {// Initialize base and exponentint base = 2, exponent = 4;// Declare variable resultint result;// Call power in main and store the returned value in resultresult = power(base, exponent);// Print value of resultcout << base << " raised to power " << exponent << " = " << result;return 0;}
Explanation
power
function
The recursive power
function takes two values of type int
in its ...