Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Program To Calculate Number Power Using Recursion In C++. The power number should always be positive integer.

#include <iostream>
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;
}
Source by www.programiz.com #
 
PREVIOUS NEXT
Tagged: #Program #To #Calculate #Number #Power #Using #Recursion #In #The #power #number #positive
ADD COMMENT
Topic
Name
6+7 =