Write a recursive method that computes the value of x^n
import java.util.Scanner; public class RecursivePower { public static double power(double x, int n) { if (n == 0) { return 1; } else { return x * power(x, n-1); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a value for x: "); double x = in.nextDouble(); System.out.print("Enter a value for n: "); int n = in.nextInt(); System.out.println(x + "^" + n + " = " + power(x, n)); } }
Get Answers For Free
Most questions answered within 1 hours.