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(−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


Read More »

By: StackOverFlow - Tuesday, 6 November

Related Posts