Fibonnaci

public class fibonacci {


public static void main(String[] args) throws NumberFormatException, IOException {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("how many numbers:");

int n=Integer.parseInt(br.readLine());

long f1=0,f2=1;
System.out.println(f1);
System.out.println(f2);

long f=f1+f2;
System.out.println(f);

int count=3;
while(count<n)
{
    f1=f2;
   f2=f;
   f=f1+f2;
   System.out.println(f);
   count++;
}

}
}

Output:

how many numbers: 5

0
1
1
2
3











No comments:

Post a Comment