Write a recursive function power (x, y) that calculates the value of x to the power.
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;
}
}
}
Comments
Leave a comment