asked 178k views
1 vote
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.

asked
User Kunjal
by
8.4k points

1 Answer

2 votes

Answer:

  1. public static double power_2(double num1, int num2){
  2. if(num2<0){
  3. return 0;
  4. }
  5. else{
  6. double returnedVal = Math.pow(num1,num2);
  7. return returnedVal;
  8. }
  9. }

Step-by-step 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
answered
User Sonjia
by
7.5k points

No related questions found