I'm unable to understand why the output is wrong.
I've tried to write a recursive code of printing Fibonacci numbers and got the expected output and a stream of unexpected values.
public class FibonacciSeries {
static int limitNum = 10; //the desired number of Fibonacci values
public static void main(String[] args) {
FibonacciSeries series = new FibonacciSeries();
series.printRecursiveFibonacci(0,1,1);
}
public void printRecursiveFibonacci(int a, int b, int count)
{
while(count<=limitNum)
{
if(count==1||count==2)
{
System.out.println(count-1);
count++;
continue;
}
int k=a+b;
a=b;
b=k;
System.out.println(b);
count++;
printRecursiveFibonacci(a, b, count);
}
}
}
The expected output is 0 1 1 2 3 5 8 13 21 34
But I got - 0 1 1 2 3 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 3 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 2 3 5 8 13 21 34 34 21 34 34 13 21 34 34 21 34 34 8 13 21 34 34 21 34 34 13 21 ...