Question: THIS IS A C++ CODE AND I NEED THIS AS A JAVASCRIPT PROGRAM CODE a) The function power1() iteratively compute (x^n) for n>=0. #include using
THIS IS A C++ CODE AND I NEED THIS AS A JAVASCRIPT PROGRAM CODE
a) The function power1() iteratively compute (x^n) for n>=0.
#include
using namespace std;
//power1() function iteratively compute (x^n) for n>=0
int power1(int x,int n)
{
int ans=1,count=0;
if(n>=0)
{
//multiply x by n times
for (int i=0;i ans = ans*x; //count by 1 help in determining number of times multiplication done count=count+1; } } cout<<"No of Multiplication: "< return ans; } int main() { int x=3; int n=3; b) #include using namespace std; int c=0; //recursive function power2() compute (x^n) int power2(int x, int n) { //increment c by 1 helps in determining number of multiplication performed //and determining number of recursive call. c=::c+1; if (n==0) return 1; else if(n>0){ return x * power2(x,n-1); } } int main() { int x=2; int n=3; cout<<"Pow "< cout<<" Number of multiplication = "<<::c-1; cout<<" Number of Recursive call = "<<::c-1; return 0; } c) #include using namespace std; int c=0,d=0; //recursive function power3() compute (x^n) int power3(int x, int n) { //increment d by 1 helps in determining number of recursive call. d=::d+1; if (n==0) return 1; //if n is even if (n%2==0 && n>0) { c=::c+1; return power3(x,(n/2))*power3(x,n/2); } //if n is odd else if(n%2!=0 &&n>0) { c=::c+2; return x*power3(x,n/2)*power3(x,n/2); } } int main(void){ int x=2; int n=2; cout<<"Pow "< cout<<" Number of multiplication = "<<::c; cout<<" Number of Recursive call = "<<::d-1; return 0; } d) multiplication for 3^32 using power1() =32 using power2()=32 using power3()=95 multiplication for 3^19 using power1() =19 using power2()=19 using power3()=50 e) for 3^32 using power2()=32 using power3()=126 for 3^19 using power2() =19 using power3() =62
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
