C++ Program to Find Power of a Give Number Using Recursion

 

C++ program to find power of a number using recursion, Find power of a number

Program:

#include <iostream>
using namespace std;

int findPower (int n, int p)    //function for calculating power
{
  if (p == 0)
    return 1;

  return n * findPower (n, p - 1);  //recursive calling the function
}

int main ()
{
  int n, p;
  
  cout << "Enter the number whose power you want to find: ";
  cin >> n;
  
  cout << "Enter its power: ";
  cin >> p;
  
  int power = findPower (n, p);

  cout << "Answer is: " << power;

  return 0;
}

Output:

Enter the number whose power you want to find: 5
Enter its power: 3
Answer is: 125
C++ Program to Find Power of a Give Number Using Recursion C++ Program to Find Power of a Give  Number Using Recursion Reviewed by Beast Coder on February 28, 2021 Rating: 5

No comments:

Powered by Blogger.