Answer to Question #255314 in Java | JSP | JSF for ninos

Question #255314

Write a recursive function power (x, y) that calculates the value of x to the power.


1
Expert's answer
2021-10-22T16:46:12-0400
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    System.out.print("Enter the value of X: ");
    int X =input.nextInt();
    System.out.print("Enter the value of Y: ");
    int Y = input.nextInt();
    int output = power(X, Y);


    System.out.println(X + "^" + Y + "=" + output);
  }


  public static int power(int X, int Y) {
    if (Y != 0) {


      return (X * power(X, Y - 1));
    }
    else {
      return 1;
    }
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS