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
import java.util.*;
class Fibonacci_Series_fun
{
public void f1(int n)
{
int a=0,b=1,c,i;
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;
}
}
public static void main(String args[])
{
int a;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to print the fibonacci series :");
a=sc.nextInt();
Fibonacci_Series_fun obj=new Fibonacci_Series_fun();
obj.f1(a);
}
}
class Fibonacci_Series_fun
{
public void f1(int n)
{
int a=0,b=1,c,i;
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;
}
}
public static void main(String args[])
{
int a;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to print the fibonacci series :");
a=sc.nextInt();
Fibonacci_Series_fun obj=new Fibonacci_Series_fun();
obj.f1(a);
}
}
Output:
Enter the number to print the fibonacci series :Fibonacci series is:
0,1,1,2,3,5,8,13,21,34,