# Recursion

• 01-19-2010, 09:30 PM
mp0667
Recursion
I can't seem to figure out how to make the iterative method into a recursive method. The iterative method is basically the square root of x n times within each other. Here is my code. The iterative method works fine but the recursion method needs work.

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter x: ");
double x=input.nextDouble();

System.out.print("Enter n: ");
double n=input.nextDouble();

System.out.print("Iteration: ");

System.out.print("Recursion: ");
System.out.println(recursion(n)) ;

}

public static double iterative(double n, double x, double answer) {

int counter=0;

while (counter<n) {

counter=counter+1;

}

}
public static double recursion(double n) {

if (n==0)
return 0;
else
return recursion(n-1);

}
}

}

Any help would be greatly appreciated.
• 01-20-2010, 08:49 AM
collin389
Code:

```public double recursion(int n, double x) { if(n==0) return x; return recursion(n-1, Math.sqrt(x)); }```