Thursday, 24 January 2013

recursive exponents

#include <iostream>

using namespace std;

int power (int, int);

int main ()

{
    int base, exp, result;

    //obtain integer from user
    cout<<"Enter the base value: ";
    cin >>base;
    cout<<"Enter the exponent value: ";
    cin>>exp;

    result = power(base, exp);

    cout<<"The result is: "<< result << endl;

    return 0;
}


// recursive definition
int power (int base, int exp)
{
    // base case
    if (exp==1)
        return base * 1;

    // recursive step
    else if (exp >-1)
        exp--;

        return base * (power(base,exp));


}

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Enterprise Project Management