Calculate base exponent, integerPower(3, 4) = 3 * 3 * 3 * 3. - C++ Data Type

C++ examples for Data Type:int

Description

Calculate base exponent, integerPower(3, 4) = 3 * 3 * 3 * 3.

Demo Code

#include <iostream>

int integerPower(int, int);

int main(int argc, const char *argv[]) {
    int base, exponent;

    std::cout << "Enter base and exponent: ";
    std::cin >> base >> exponent;

    std::cout << base << " to the power of " << exponent << ": "
              << integerPower(base, exponent) << std::endl;

    return 0;// w w w  . ja  v  a 2s. c om
}
// returns base to the power of exponent
int integerPower(int base, int exponent) {
    // base case
    if (exponent <= 0) return 1;

    return base * integerPower(base, exponent - 1);
}

Result


Related Tutorials