Computer Science, asked by anagasatyasri710, 9 months ago

Write a program to compute a^n (a power n) using recursion. INPUT & OUTPUT FORMAT: Input consists of 2 integers Refer sample input and output for formatting specifications

Answers

Answered by pranalipatil3620
5

Answer:

#include <iostream>

using namespace std;

int power(int n1, int n2);

int main() {

   int a, n, result;

   cout<<"Enter the value of a";

   cin>>a;

   cout<<"\nEnter the value of n";

   cin>>n;

   result = power(a, n);

   cout<<"\nThe value of "<<a<<" power "<<n<<" is "<<result;

   return 0;

}

int power(int a, int n) {

   if (n != 0)

       return (a * power(a, n - 1));

   else

       return 1;

}

Explanation:

Answered by LoneWolf225
0

Answer:

#include<iostream>

using namespace std;

int p(int a,int n)

{

 if(n>0)

   return a*p(a,n-1);

 else

   return 1;

}

int main()

{

int a,n;

 std::cin>>a>>n;

 printf("Enter the value of a\nEnter the value of n\n");

 printf("The value of %d power %d is %d",a,n,p(a,n));

}

Explanation:

Similar questions