Write a recursive method that calculates the exponentiation function that given
ID: 3582321 • Letter: W
Question
Write a recursive method that calculates the exponentiation function that given integer two values (a, b), returns a raised to the b power. You may assume only positive values will be given. You need not optimize your solution. We're looking for a very simple implementation here. What is/are the base case(s): ___ How will the parameter(s) get smaller upon each recursive call? ___ YOUR CODE MUST BE RECURSIVE. Do not use any loops (while, do/while, or not). Do not declare any variables outside of the method. You may declare local variables inside the method. public static ___ pow (___)Explanation / Answer
base cases: if power is 0,return 1
1st Parameter will remain same second parameter will decrease by 1 after each recursive call.
public static int pow(int a,int b){
if(b==0) //base case
return 1;
else
return a*pow(a,b-1); //calling recursively
}
Note:Please ask in case of any doubt,thanks.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.