Insert the missing code in the following code fragment. This fragment is intended to recursively compute xn, where x and n are both non-negative integers:
public int power(int x, int n)
{
if (n == 0)
{
____________________
} else
{
return x * power(x, n - 1);
} }
a) return 1;
b) return x;
c) return power(x, n - 1);
d) return x * power(x, n - 1);
Solution
And: a) return 1;
public int power(int x, int n){
// base case, zero power of any number is 1
if(n==0){
return 1;
}
else{
return x*pow(x, n-1);
}
}
.