05 September, 2012

Fibonacci by Recursive Functions

Write a java program that uses recursive functions

import java.lang.*;
import java.util.*;
class FibonacciRec
{
   public static void main(String args[])
   {
      int a=0,b=1;
      Recurssion fr= new Recurssion();
      Scanner s = new Scanner(System.in);
      System.out.println("Enter length of series");     

      int n1= s.nextInt();
      int n=n1-3;
      System.out.println("Fibonacci series for "+n1+" is");
      System.out.print(" "+a+" "+b+" ");
      fr.recurssion(n,a,b);
   }
}
class Recurssion
{
  int i=0;
  public void recurssion(int n,int a ,int b)
  {
      int c;
      if(i<=n)
      {
        c=a+b;
        a=b;
        b=c;
        System.out.print(c+" ");
        i++;
        recurssion(n,a,b);
      }
   }
}

No comments:

Post a Comment

Thank You !