0

I am trying to do the following program in Java where I'm writing a recursive and an iterative method to compute the sum of all odd numbers from n to m

import java.util.Scanner;

public class AssignmentQ7 {

public static int recursivesum(int n, int m){

    if (n < m){
        int s = n;
        s += recursivesum(n+2, m);

    } else{
        if(m < n){
            int  s = m;
            s += recursivesum(m+2, n);

        }
    }
    return s;
}

public static int iterativesum(int n, int m){
    if(n < m){
        int s = n;
        for(int i = n; i <= m; i += 2){
            s += i; 
                    return s;
        }
    } else
        if(m < n){
            int s = m;
            for(int i = m; i <= n; i += 2){
            s += i; 
                    return s;
            }
        }

}

public static void main(String args[]){

    int n,m;

    Scanner in = new Scanner(System.in);

    System.out.println("Enter two numbers: ");
    n = in.nextInt();
    m = in.nextInt();

    while(n%2 == 0){
        System.out.println("Enter the first number again: ");
        n = in.nextInt();
    }

    while(m%2 == 0){
        System.out.println("Enter the second number again: ");
        m = in.nextInt();
    }

    System.out.println("The answer of the recursive sum is: " + recursivesum(n,m));
    System.out.println("The answer of the iterative sum is: " + iterativesum(n,m));
}
 }

I'm getting an error cannot find symbol - variable enter code heres. I don't know what's wrong! Can anyone help please?

4
  • 1
    scope of s is with in the braces...define it outside as class variable Commented Mar 25, 2014 at 9:35
  • @VinayVeluri: There's no need for it to be a class variable. Commented Mar 25, 2014 at 9:36
  • declare your s variable outside your if condition Commented Mar 25, 2014 at 9:36
  • @JonSkeet true!! any variable whose scope doesn't end before return. Commented Mar 25, 2014 at 9:48

10 Answers 10

5

This method is the problem:

public static int recursivesum(int n, int m) {
    if (n < m) {
        int s = n; // Variable declared here... scoped to the if
        s += recursivesum(n+2, m);

    } else {
        if (m < n) {
            int  s = m; // Variable declared here... scoped to this if
            s += recursivesum(m+2, n);
        }
    }
    return s; // No such variable in scope!
}

You could use:

public static int recursivesum(int n, int m) {
    int s = 0; // See note below
    if (n < m) {
        s = n + recursivesum(n+2, m);

    } else {
        if (m < n) {
            s = m + recursivesum(m+2, n);
        }
    }
    return s;
}

We have to give s an explicit initial value, because you currently don't have any code handling the case where n and m are equal. It's not clear what you want to do then.

Another alternative is to return from the if statements, just as you do in iterativesum... although you'll again need to think about what to do if m == n:

public static int recursivesum(int n, int m) {
    if (n < m) {
        // You don't even need an s variable!
        return n + recursivesum(n+2, m);   
    } else if (m < n) {
        // Simplified else { if { ... } } to else if { .... }
        return m + recursivesum(m+2, n);
    } else {
        // What do you want to return here?
    }
}

Note that you've got the same problem in iterativesum - the compiler should be complaining at you at the moment that not all paths return a value. What do you expect iterativesum(3, 3) to do?

Sign up to request clarification or add additional context in comments.

Comments

2

in recursivesum(int n, int m) method, you have declared s within if condition, but, you tried to access it in else part.

public static int recursivesum(int n, int m){
    int s = n; // Now s having method local scope

    if (n < m){
        s += recursivesum(n+2, m);

    } else{
        if(m < n){
            int  s = m;
            s += recursivesum(m+2, n);

        }
    }
    return s;
}

Comments

0

In the recursivesum(int n,int m) function the scope of variable s is inside the if and else block. When your returning s it is out of scope.

Try using some IDE's like eclipse. So that you can debug these errors instantly

Comments

0

Try this:

int s;
if (n < m){
        s = n;
        s += recursivesum(n+2, m);

    } else{
        if(m < n){
            int  s = m;
            s += recursivesum(m+2, n);

        }
    }
    return s;

Comments

0

You are declaring variable s inside if statement, that is why you get such error. Start from declaration int s outside if statement.

Comments

0

your s is out of scope in recursivesum(int n, int m) method

Comments

0

Declare s outside the if-else block

Comments

0

It's scope problem. You're declaring variable s inside the if statements which is (local definition) of variable.

You need to modify the two methods as follows:

The recursive method will be

public static int recursivesum(int n, int m) {
    int s = 0;
    if (n < m) {
        s = n;
        s += recursivesum(n + 2, m);
    } else {
        if(m < n){
            s = m;
            s += recursivesum(m + 2, n);
        }
    }
    return s;
}

And the iterative method will be:

public static int iterativesum(int n, int m) {
    int s = 0;
    if(n < m) {
        s = n;
        for(int i = n; i <= m; i += 2) {
            s += i;
        }
    } else {
        if(m < n) {
            s = m;
            for(int i = m; i <= n; i += 2) {
                s += i;
            }
        }
     }
     return s;
}

Comments

0

You have an error in the first method where you define s outside the scope which you return it from. In the second method you return inside the loop.

As others in this thread suggests, use an IDE like Eclipse (https://www.eclipse.org/) or IntelliJ (http://www.jetbrains.com/idea/)

import java.util.Scanner;

public class AssignmentQ7  {

  public static int recursivesum(int n, int m) {
    int s = n;
    if (n < m) {
      s += recursivesum(n + 2, m);
    }
    else {
      if (m < n) {
        s = m;
        s += recursivesum(m + 2, n);
      }
    }
    return s;
  }

  public static int iterativesum(int n, int m) {
    int s = 0;
    if (n < m) {
      for (int i = n; i <= m; i += 2) {
        s += i;
      }
    }
    else if (m < n) {
      for (int i = m; i <= n; i += 2) {
        s += i;
      }
    }
    return s;
  }

  public static void main(String args[]) {

    int n, m;

    Scanner in = new Scanner(System.in);

    System.out.println("Enter two numbers: ");
    n = in.nextInt();
    m = in.nextInt();

    while (n % 2 == 0) {
      System.out.println("Enter the first number again: ");
      n = in.nextInt();
    }

    while (m % 2 == 0) {
      System.out.println("Enter the second number again: ");
      m = in.nextInt();
    }

    System.out.println("The answer of the recursive sum is: " + recursivesum(n, m));
    System.out.println("The answer of the iterative sum is: " + iterativesum(n, m));
  }
}

Comments

0

Your code must be like this, you dont have to use two for loop.

import java.util.Scanner;

public class AssignmentQ7 {

    public static int recursivesum(final int n, final int m) {

        final int lower = n < m ? n : m;
        final int upper = n > m ? n : m;
        final int total = lower;

        if (lower >= upper) {
            return total;
        }
        return total + AssignmentQ7.recursivesum(lower + 2, upper);
    }

    public static int iterativesum(final int n, final int m) {

        final int lower = n < m ? n : m;
        final int upper = n > m ? n : m;

        int total = 0;
        for (int num = lower; num <= upper; num = num + 2) {
            total += num;
        }

        return total;
    }

    public static void main(final String args[]) {

        int n, m;

        final Scanner in = new Scanner(System.in);

        System.out.println("Enter two numbers: ");
        n = in.nextInt();
        m = in.nextInt();

        while (n % 2 == 0) {
            System.out.println("Enter the first number again: ");
            n = in.nextInt();
        }

        while (m % 2 == 0) {
            System.out.println("Enter the second number again: ");
            m = in.nextInt();
        }

        System.out.println("The answer of the recursive sum is: " + AssignmentQ7.recursivesum(n, m));
        System.out.println("The answer of the iterative sum is: " + AssignmentQ7.iterativesum(n, m));
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.