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?