Program to calculate the power of any number using recursion in C++
Solution:
#include <iostream> using namespace std; long int calculate(int x,int y) { long int result=1; if(y == 0) return result; else return x*(calculate(x,y-1)); } int main() { int n,a,ans; cout<<"Enter a number "; cin>>n; cout<<"Enter power : "; cin>>a; ans=calculate(n,a); cout<<"Answer is "<<ans; return 0; }
Output :
Enter a number 5 Enter power : 3 Answer is 125