Exercises 1


There is no way to raise a number to the n’th power built into C++.

Write a recursive function power( base, exponent), to do the calculation and return the result: baseexponent.


power(3,4) = 34 = 3*3*3*3

First - sketch your function on paper. Next – implement a program to test it. Last - make a drawing to show the function-calls involved in calculating 34.

See Solution:

function Power(a,b){ var i, total = a; for(i = 0; i<b-1; i++){ total *= a; } return total; } function PowerRecusive(a,b){ if(b===1){ return a; }else{ return a*PowerRecusive(a,b-1); } } a^b

Result :