Consider the following incomplete C program that calculates and prints the resul
ID: 3788055 • Letter: C
Question
Consider the following incomplete C program that calculates and prints the result of base raised to power of exponent. i.e.. base^exponent, where exponent greaterthanorequalto 0. In mathematical form (for simplicity, base = b. exponent = e). b^e = {1, if e = 0 b times b^e -1, if e greaterthanorequalto 1 Define function power 1 that implements b^e in the iterative approach. Using a suitable repetition structure, power 1 performs multiplication on b repeatedly, for e times. For example, power 1 (5, 3) returns the result of 5*5*5. i.e.. 125. (ii) Define the function power 2 that implements the recursive version of be. The recursive algorithm works as below. (b) Complete Table Q1 that compares recursive and iterative functions.Explanation / Answer
Code:
#include<stdio.h>
using namespace std;
int power1(int,int);
int power2(int,int);
int main()
{
printf("5^3=%d ",power1(5,3));
printf("5^3=%d ",power2(5,3));
return 0;
}
int power1(int b,int e)
{
int i;
int res=1;
for(i=0;i<e;i++)
{
res*=b;
}
return res;
}
int power2(int b,int e)
{
if(e==0)
return 1;
else
return b*power2(b,e-1);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.