Fibonacci series program in java

Learn Java Programming for Beginners

Fibonacci series program in java

Fibonacci series program in java

import java.util.*;
class Fibonacci_series
{
public static void main(String args[])throws Exception
{
int f,q,i=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number: ");       //input how many no is Fibonacci series
f=sc.nextInt();

System.out.println("-------------------Fibonacci series------------------");

int a=0,b=1;
System.out.println("Fibonacci series : "+a);
System.out.println("Fibonacci series : "+b);
while(i!=f-2)
{
q=a+b;
a=b;
b=q;
System.out.println("Fibonacci series : "+q);
i++;

}
}
}
/*

Fibonacci series program in java Output:-

C:\Documents and Settings\Administrator\Desktop\AOAD>javac Fibonacci_series.java

C:\Documents and Settings\Administrator\Desktop\AOAD>java Fibonacci_series
Enter a number:
5
Fibonacci series : 0
Fibonacci series : 1
Fibonacci series : 1
Fibonacci series : 2
Fibonacci series : 3
Fibonacci series : 5

*/

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *