In: Computer Science
Write a recursive method pow(x, y) to calculate xy, where x and y are positive integers. If x=2, y=4, the method pow should return 16. Java answers only please.
import java.util.*;
public class Main {
public static int calculatePower(int a,int b)
{
if(b==0) return 1;
else if(b==1) return a;
else
return a * calculatePower(a,b-1);
}
public static void main(String[] args) {
Scanner scr=new Scanner(System.in);
int x,y,z;
System.out.print("\nTo calculate x^y , value of x :");
x=scr.nextInt();
System.out.print("\nValue of y :");
y=scr.nextInt();
z=calculatePower(x,y);
System.out.print("\nx^y :"+z);
}
}
If you have any questions comment down. Please don't simply downvote and leave. If you are satisfied with answer, please? upvote thanks