Write a program to compute a^n (a power n) using recursion
Answers
Answered by
1
Answer:
Example: Program to Computer Power Using Recursion
#include <iostream>
using namespace std;
int calculatePower(int, int);
int main()
{
int base, powerRaised, result;
cout << "Enter base number: ";
cin >> base;
cout << "Enter power number(positive integer): ";
cin >> powerRaised;
result = calculatePower(base, powerRaised);
cout << base << "^" << powerRaised << " = " << result;
return 0;
}
int calculatePower(int base, int powerRaised)
{
if (powerRaised != 0)
return (base*calculatePower(base, powerRaised-1));
else
return 1;
Similar questions
English,
4 months ago
Math,
4 months ago
English,
9 months ago
Computer Science,
9 months ago
Math,
1 year ago