Java program for Recursion Fibonacci Series

Java program for Recursion Fibonacci Series


Recursion Fibonacci Series 


Fibonacci series or numbers denoted by Fn. It is a series of numbers formed by the addition of the preceding two numbers in the series. The first two terms are 0 and 1 respectively. The terms after this are generated by simply adding the previous two terms.
For example of Fibonacci series: 0,1,1,2,3,5,8,13….etc.
/**
* Recursion Fibonacci Series 
*/
import java.io.*;
class RecFibonacci
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int a,b,c,l;
RecFibonacci() //Constructor
    {
     a=0;
     b=1;
     c=0;
     l=0;
    }
void input()throws IOException //Function to input the limit
    {
     System.out.print("Enter the limit : ");
     l=Integer.parseInt(br.readLine());
    }
int fib(int n) //
    {
    if(n<=1)
    return a;
    else if(n==2)
    return b;
    else
    return (fib(n-1)+fib(n-2));
    }
void fiboSeries() //Function generating all the Fibonacci Series numbers upto 'n' terms
    {
        System.out.println("The Fibonacci Series is:");
        for(int i=1;i<=l;i++)
        {
            c=fib(i);
            System.out.print(c+"  ");
        }
    }
public static void main()throws IOException
  {
   RecFibonacci ob=new RecFibonacci();
   ob.input();  
   ob.fiboSeries();
  }
}


More Program


Recursion program in Java

Java program for Stack




SHARE THIS
Previous Post
Next Post