Results 1 to 2 of 2

Thread: Recursion

  1. #1
    mp0667 is offline Member
    Join Date
    Jan 2010
    Rep Power

    Default 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(;
    double answer=0;

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

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

    System.out.print("Iteration: ");
    System.out.println(iterative(n, x, answer));

    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) {

    answer= Math.sqrt(x+answer);


    return answer;
    public static double recursion(double n) {

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



    Any help would be greatly appreciated.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Rep Power


    Java Code:
    public double recursion(int n, double x)
    if(n==0) return x;
    return recursion(n-1, Math.sqrt(x));

Similar Threads

  1. recursion and tail-recursion differences
    By OptimusPrime in forum New To Java
    Replies: 2
    Last Post: 12-28-2009, 07:26 PM
  2. Recursion
    By kathyla18 in forum New To Java
    Replies: 2
    Last Post: 04-09-2009, 03:26 AM
  3. Recursion
    By jachandru in forum New To Java
    Replies: 1
    Last Post: 01-24-2009, 01:52 PM
  4. Recursion
    By Mika in forum New To Java
    Replies: 5
    Last Post: 01-04-2009, 02:13 AM
  5. Please help with recursion
    By pheonix in forum New To Java
    Replies: 9
    Last Post: 12-27-2008, 12:41 PM

Posting Permissions

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