Question #19226

int foo(int x, int n)
{
int val = 1;
if (n > 0)
{
if (n % 2 == 1)
val *= x;
val *= foo(x * x, n / 2);
}
return val;
}
What function of x and n is computed by foo?
· xn
· x * n
· nx
· None of the above

Expert's answer

#include<iostream>

using namespace std;

int foo(int x, int n)

{

int val = 1;

if (n > 0)

{

if (n % 2 == 1)

val *= x;

val *= foo(x * x, n / 2);

}

return val;

}

int main ()

{

cout << foo(2,3) << endl;

system ("pause");

return 0;

}

**Answer**: None of the above

using namespace std;

int foo(int x, int n)

{

int val = 1;

if (n > 0)

{

if (n % 2 == 1)

val *= x;

val *= foo(x * x, n / 2);

}

return val;

}

int main ()

{

cout << foo(2,3) << endl;

system ("pause");

return 0;

}

## Comments

## Leave a comment