Sunday, May 5, 2013

The Fibonacci sequence is defined by the following rule. The first 2 values in the sequence 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 nth value of the Fibonacci sequence.


/* Fibonacci series Program in Java */

/*1.Fibonacci program using Non-Recursive Functions*/

import java.lang.*;
import java.io.*;
class Fibonacci
{
    public static void main(String args[])throws IOException
    {
    int i,sum=0,f=1,res,n;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Hey boss This is Fibonacci series Generator program");
    System.out.println("Enter The range");
    n=Integer.parseInt(br.readLine());
    System.out.println("The fibonacci series is:");
    for(i=0;i<n;i++)
    {
        res=sum+f;
        System.out.println(sum);
        sum=f;
        f=res;
    }
    System.out.println("\nTask completed.");
    }
}

Output:

Hey boss This is Fibonacci series Generator program
Enter The range
20
The fibonacci series is:
0
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181

Task completed.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More