I'm attempting to print the Fibonacci series using this application. I used n=8 (the number of Fibonacci numbers to print), and I received more than ten. Could you just point out where I'm going wrong?
import java.util.*;
class myprogram{
static int f=0,s=1,sum=0;
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.printf("Enter the number of numbers you want to print");
int n=sc.nextInt();
System.out.print(f+" "+s);
fib((n-2));
}
static void fib(int count){
while(count!=0)
{
sum=f+s;
System.out.print(" "+sum);
f=s;
s=sum;
count-=1;
fib(count);
}
}
}
Input:
n=8
Output expected:
0 1 1 2 3 5 8 13 21 33
My result:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6763 10946 17711 28657 46368 75025 121393 ...