FIBONACCI Series

import java.io.*;
public class Fibonacci
{
public static void main(String[] args)throws IOException
{
int a=-1,b=1,c=0,s,i;
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter Number of terms for Fibonacci :-");
s= Integer.parseInt(br.readLine());

for(i =1;i<=s;i++)
{
c= a+b; // Generating Fibonacci Series
System.out.print(c + " ");
a= b; // assigning the value of b to a
b =c; // assigning the value of c to b
}
}

}

/*
Example :-
Enter Number of terms for Fibonacci :-
Input :-
10
Output :-

0 1 1 2 3 5 8 13 21 34
*/

Comments

Popular Posts