📜  在 C++ 中使用递归计算数幂的程序.幂数应始终为正整数. - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:57.788000             🧑  作者: Mango

代码示例1
#include 
using namespace std;

int calculatePower(int, int);

int main()
{
    int base, powerRaised, result;

    cout << "Enter base number: ";
    cin >> base;

    cout << "Enter power number(positive integer): ";
    cin >> powerRaised;

    result = calculatePower(base, powerRaised);
    cout << base << "^" << powerRaised << " = " << result;

    return 0;
}

int calculatePower(int base, int powerRaised)
{
    if (powerRaised != 0)
        return (base*calculatePower(base, powerRaised-1));
    else
        return 1;
}