Write the definition of a function power_to, which receives two parameters. The first is a float and the second is an integer. The function returns a float. If the second parameter is negative, the function returns zero. Otherwise it returns the value of the first parameter raised to the power of the second.
public static double power_2(double num1, int num2){
if(num2<0){
return 0;
}
else{
double returnedVal = Math.pow(num1,num2);
return returnedVal;
}
}
Explanation:
This is implemented in Java programming language
Line 1 Is the method declaration which indicates that it will return a floating point number (type double) and accepts two parameters, the first a double and the second an int as required by the question
Line 2 Uses and if statement to check if the second parameter is negative (less than 0) and returns 0.0 if this is true
Lines 5-7 is the else condition that uses java's Math.pow method to raise the num1 to the power num2 and returns the value