C++ Program to Find Power of Number using Recursion

C++ program to find power of number using recursion.


C++ Program to calculate exponential of a number using recursion

#include <iostream>

using namespace std;

int getPower(int base, int exponent);

int main(){
    int base, exponent, counter, result = 1;
    cout << "Enter base and exponent\n";
    cin >> base >> exponent;
     
    result = getPower(base, exponent);
     
    cout << base << "^" << exponent << " = " << result;
    return 0;
}

int getPower(int base, int exponent){
    /* Recursion termination condition,
     * Anything^0 = 1
     */
    if(exponent == 0){
        return 1;
    }
    return base * getPower(base, exponent - 1);
}
Output
Enter base and exponent
3 4
3^4 = 81

Recommended Posts
C++ program to Find Factorial of a Number Using Recursion
C++ Program to Display Fibonacci Series using Loop and Recursion
C++ Program to Find Sum of Natural Numbers Using Recursion
C++ Program to Find GCD or HCF of Two Numbers Using Recursion
C++ Program to find length of string
C++ Program to Calculate Difference Between Two Time Periods
C++ Program to Print Pascal Triangle
C++ Program to Count Words in Sentence
C++ Program to Check Prime Number Using Function
All C++ Programs