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
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
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
Hindi,
4 months ago
Accountancy,
4 months ago
English,
4 months ago
Math,
9 months ago
Social Sciences,
9 months ago
Physics,
1 year ago
Hindi,
1 year ago