Q:

A recursive definition of a n where a is an integer and n is a non-negative integer follows:

0

A recursive definition of a n  where a is an integer and n is a non-negative integer follows:

a n = 1 if n == 0 = a * a n-1 if n > 0 Write a recursive function called mypower, which receives a and n and returns the value of a n by implementing the previous definition. Note: The program should NOT use ^ operator anywhere; this is to be done recursively instead! Test the function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

mypower.m

function res = mypower(a,n)

% recursively finds a^n

% Format of call: mypower(a,n)

% Returns a^n

if n == 0

 res = 1;

else

 res = a * mypower(a,n-1);

end

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now