Implement the recursive approach in JAVA to raising a number to a power.
Write a recursive power function and a main routine to test it.
So 28 = 2 * 27 = 2 * 2 * 26 and so on.
So x^Y is x multiplied by itself y times.
Recursive approach in JAVA :
public class Main { public static void main(String[] args) { int x=2; int y=8; System.out.println("Value of ( "+x+" ^ " +y+" ) is : " +power(x,y)); } public static int power(int x , int y) { if(y==0) return 1; int n = power(x , y -1); int ans = n*x ; return ans; } }
SCREENSHOT OF ABOVE CODE:
EXAMPLE :
Get Answers For Free
Most questions answered within 1 hours.