Sunday, May 5, 2013

The Fibonacci sequence is defined by the following rule. The first 2 values in the sequenc are 1,1. Every subsequent value is the sum of the 2 values preceding it. Write A Java Program (WAJP) that uses both recursive and non-recursive functions to print the n th value of the Fibonacci sequence.



/*1.Fibonacci program using Recursive Functions*/
import java.lang.*;
import java.io.*;
class FibonacciRecursive
{
    public static void main(String args[])throws IOException
    {
    int n;
    BufferedReader d=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter The range");
    n=Integer.parseInt(d.readLine());
    System.out.println("The fibonacci series is:");
    Fibonaccino(n);//funcion Calling
    }//Main method End.
  
    static void Fibonaccino(int n)
    {
        int i,sum=0,f=1,res;  
        for(i=0;i<n;i++)
        {
        res=sum+f;
        System.out.println(sum);
        sum=f;
        f=res;
        }
        System.out.println("\nTask completed.");
    }
}

Output:

E:\javamani>java FibonacciRecursive
Enter The range
10
The fibonacci series is:
0
1
1
2
3
5
8
13
21
34
Task completed.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More