C++ Program to Find Power of Number using Recursion

In the previous article, we have discussed C++ Program to Find GCD or HCF of Two Numbers Using Recursion. In this article, we will see 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;
}
/*
* Function to calculate base^exponent using recursion
*/
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

Print Hello World, Multiply two Numbers, Add Two Numbers, etc. are some of the basic-level Programs in C++. Want to practice advanced C++ Programs? Go with the available links & gain more coding knowledge.