Fibonacci
Series generates subsequent number by adding two previous numbers.
Fibonacci series starts from two numbers − F0 and F1. The initial
values of F0 and F1 can be taken 0, 1 or 1, 1 respectively.
Fn = Fn-1 + Fn-2
Fn = Fn-1 + Fn-2
Algorithm
1. Take integer variable A, B, C
2. Set A = 1, B = 1
3. DISPLAY A, B
4. C = A + B
5. DISPLAY C
6. Set A = B, B = C
7. REPEAT from 4 - 6, for n times
1. Take integer variable A, B, C
2. Set A = 1, B = 1
3. DISPLAY A, B
4. C = A + B
5. DISPLAY C
6. Set A = B, B = C
7. REPEAT from 4 - 6, for n times
Program:
import java.util.*;class Fibonacci_Series
{
public static void main(String args[])
{
int n,a=0,b=1,c,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to print the fibonacci series :");
n=sc.nextInt();
System.out.println("Fibonacci series is:");
System.out.print(a+","+b+",");
for(i=3;i<=n;i++)
{
c=a+b;
System.out.print(c+",");
a=b;
b=c;
}
}
}
Output:
Enter the number to print the fibonacci series :
Fibonacci series is:
0,1,1,2,3,5,8,13,21,34,
Fibonacci series is:
0,1,1,2,3,5,8,13,21,34,