# Recursion help

• March 17th, 2011, 03:36 PM
Actinistia
Recursion help
Hi everyone, so I'm still trying to figure out how to get my recursive function to work to calculate my sine so I figured I'd start with a factorial function and work my way up, adding the necessary parts. I looked at http://www.javaprogrammingforums.com...-concepts.html but I can't get any of the code to work.

I get these errors
Code java:

```ftest.java:1: class, interface, or enum expected public static int interativeFactorial(int n) ^ ftest.java:4: class, interface, or enum expected for(int i = 1; i < n; i++) ^ ftest.java:4: class, interface, or enum expected for(int i = 1; i < n; i++) ^ ftest.java:4: class, interface, or enum expected for(int i = 1; i < n; i++) ^ ftest.java:7: class, interface, or enum expected } ^ ftest.java:9: class, interface, or enum expected } ^ 6 errors```

All I'm using is

Code java:

```public static int interativeFactorial(int n) { int answer = 1; for(int i = 1; i < n; i++) { answer *= i; } return answer; } }```

Code java:

```public class ftest { public static int interativeFactorial(int n) { int answer = 1; for(int i = 1; i < n; i++) { answer *= i; } return answer; } }```

but then I get the error

Code java:

`Exception in thread "main" java.lang.NoSuchMethodError: main`

so then I tried putting in
Code java:

```public class ftest { public static void main(String args[]) { public static int interativeFactorial(int n) { int answer = 1; for(int i = 1; i < n; i++) { answer *= i; } return answer; } } }```

then I just get more errors!
Code java:

```ftest.java:5: illegal start of expression public static int interativeFactorial(int n) ^ ftest.java:5: illegal start of expression public static int interativeFactorial(int n) ^ ftest.java:5: ';' expected public static int interativeFactorial(int n) ^ ftest.java:5: '.class' expected public static int interativeFactorial(int n) ^ ftest.java:5: ';' expected public static int interativeFactorial(int n) ^ 5 errors```

I'm pretty lost about what to do at this point if I can't even get this to compile and work right. Any clarification would be appreciated!
• March 20th, 2011, 03:41 AM
sunde
Re: Recursion help
First off this is not recursion, you are using a loop, recursion relies on recalling the same method until some condition is met.

Your code, although not recursion is fine, except you need a main method. All classes are a series of methods, main is the entry point of a program. You don't define a method in main, instead you call methods in main. Methods are created outside of the main method

Code java:

```public class blah{ public static void hello(){ System.out.println("hello"); } public static void main(String[] args){ hello(); } }```

Try compiling and runnning that and see if it helps you a bit with what you did wrong in your code.
• March 20th, 2011, 04:55 AM
sunde
Re: Recursion help
This is an example of a program to recursively add two numbers.

Code java:

```public class Add{ public static int add(int x, int y){ if(y == 0){ return x; } else{ return add(x + 1, y - 1); } } public static void main(String[] args){ System.out.println(add(5, 6)); } }```
• March 21st, 2011, 12:26 PM
Actinistia
Re: Recursion help
Quote:

First off this is not recursion, you are using a loop, recursion relies on recalling the same method until some condition is met.

Your code, although not recursion is fine, except you need a main method. All classes are a series of methods, main is the entry point of a program. You don't define a method in main, instead you call methods in main. Methods are created outside of the main method

Code java:

```public class blah{ public static void hello(){ System.out.println("hello"); } public static void main(String[] args){ hello(); } }```

Try compiling and runnning that and see if it helps you a bit with what you did wrong in your code.

Thanks for clarifying that, I stated that it was recursion based off the topic of the tutorial I found the code in. The example code was very helpful to show how recursion actually works. I think I understand why it didn't work now. I'm still pretty new to java and all, so thanks you for all your help!