1. Member Join Date
Jan 2010
Posts
1
Rep Power
0

## 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.  Reply With Quote

2. Senior Member Join Date
Nov 2009
Posts
236
Rep Power
11

## Java Code:
```public double recursion(int n, double x)
{
if(n==0) return x;
return recursion(n-1, Math.sqrt(x));
}```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•