# First Timer using recursion

Ok so I got a project using recursion and It's a little hard to comprehend but I need some help. I am trying to take user input and pass it into the method f and then It uses recursion on the line f(n)= 2(2n+1)*f(n?1)/ n+2. and then printing out what F(n) equals but its giving me an error while trying to set up the line in the method f. A sample run of the project would be like 10 is n and the f(n) = 58786. Please help me

```
import java.util.Scanner;
public class Recursive
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer for n");
long n =input.nextLong();
System.out.println("f(n) = " + f(n));
}
public static long f(long n)
{
if (n <= 0) return (n=1);
if (n > 0) return (2(2n+1)*f(n?1) n+2 );
}
```

}

Kartik
answered question

### 1 Answer

Replace

```
if (n > 0) return (2(2n+1)*f(n?1) n+2 );
```

with

```
return 2 * (2 * n + 1) * f(n - 1) / (n + 2);
```

In java, `2n`

is not equal to `2*n`

. You need to explicitly put that multiplication asterisk sign. Same with brackets.

Full method is:-

```
public static long f(long n) {
if (n <= 0) return 1;
return 2 * (2 * n + 1) * f(n - 1) / (n + 2);
}
```

It prints `58786`

for the input `10`

.

Kartik
posted this

## Have an answer?

JD

And the error is....?