# How to make a negative Fibonacci sequence in Java using recursion?

I have to write a program that returns Fibonacci number, but not only positive. I don't know what is not right in the way I've wrote it, but my code works with positive numbers and not with negative ones.

public static int negFib(int n) {

if(n==0 || n==1) {

return n;

}

if(n==-1) {

return 1;

}

if(n<0 && n%2==0) {

int a = Math.abs(n);

//return negFib(n+2) - negFib(n+1);

return (-1<<(n+1))*(negFib(n-1,) + negFib(n-2); // Fibonacci negative

//F(n)=F(n+2)−F(n+1)

//F(−1)=F(1)−F(0)=1−0=1 , F(−2)=F(0)−F(1)=0−1=−1

}

return negFib(n-1) + negFib(n-2); //Fibonacci positive

}...

By: StackOverFlow - Tuesday, 6 November

# Related Posts

- Older News
- Java recursion how to understand the given statements StackOverFlow (Yesterday) - I was googling about dynamic programming when i came to this piece of code:int fib (int n) {if (n < 2)return 1;return fib(n-1) + fib(n-2);}When I run it, it gives...
- Library to control program sequence with GUI elements StackOverFlow (Yesterday) - I am currently working on a tool to automate mass objects in a certain system. The system is not important to know.Is there any library to solve these problems:Provide a...
- Recursion inside ActionListener StackOverFlow (3 days ago) - I have to add add recursion inside ActionListener to calculate fibonacci sequence of a given number, i've tried to add recursion in this way, but it didn't work.fibonacci.addActionListener(new ActionListener() {@Overridepublic...
- Bit wise negative operation [duplicate] StackOverFlow (4 days ago) - This question already has an answer here:Shifting by Negative Numbers2 answersBit wise shift operator with shift by negative number1 answerJava Bitwise shift operator with Negative Operands2 answersWhile looking on the...