Explore BrainMass
Share

Explore BrainMass

    Recursion implementation

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Rewrite the following power() function so that it uses recursion. Provide a function main to test your implementation.

    #include <iostream>

    using namespace std;
    int power (int number, int exponent)
    {
    int retval = 1;

    for (int i = 0; i < exponent; i++)
    {
    retval = retval * number;
    }

    return retval;
    }

    int main (int argc, char **argv)
    {
    cout << power(2,3) << endl;
    cout << power(3,6) << endl;
    cout << power(4,9) << endl;

    return 0;
    }

    © BrainMass Inc. brainmass.com October 10, 2019, 12:51 am ad1c9bdddf
    https://brainmass.com/computer-science/algorithms/recursion-implementation-310469

    Solution Summary

    The expert writes a power function for recursion implementation.

    $2.19