Question: Write code to complete raiseToPower(). Sample output if userBase is 4 and userExponent is 2 is shown below. Note: This example is for practicing recursion;
Write code to complete raiseToPower(). Sample output if userBase is 4 and userExponent is 2 is shown below. Note: This example is for practicing recursion; a non-recursive method, or using the built-in method pow(), would be more common.
4^2 = 16
public class ExponentMethod { public static int raiseToPower(int baseVal, int exponentVal) { int resultVal = 0;
if (exponentVal == 0) { resultVal = 1; } else { resultVal = baseVal * /* Your solution goes here */; }
return resultVal; }
public static void main (String [] args) { int userBase = 0; int userExponent = 0;
userBase = 4; userExponent = 2; System.out.println(userBase + "^" + userExponent + " = " + raiseToPower(userBase, userExponent));
return; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
