Search Tutorials

Tuesday, 23 April 2013

Java code to generate Fibonacci series using recursion

Java program to generate Fibonacci series containing given number of terms using recursion.

import java.io.*;
class Fibonacci
{
 public static void main(String args[]) throws IOException
 {
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 Fibonacci call = new Fibonacci();
 System.out.print("Enter no. of terms : ");
 int n = Integer.parseInt(br.readLine());
 call.generate(n);
 }
 int fibo(int n)
 {
 if(n==1)
  return 1;
 else if(n==2)
  return 1;
 else
  return fibo(n-2)+fibo(n-1);
 }
 void generate(int n)
 {
 for(int i=1;i<=n;i++)
  System.out.println(fibo(i));
 }
}

ALGORITHM:-

1. Start

2. Accept the number of terms of the Fibonacci series from the user.

3. A particular term of the series is the sum of its previous two terms.

4. Using method recursion generate the terms of the series.

5. Print the Fibonacci series up to the number of terms as required by the user.

5 comments:

  1. Pretty well post, though you could do it better if would use ​essay writer​. Try it and you won't sorry, I'm sure of that

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. High-quality essays on the topics you need on the best conditions and as soon as possible.

    ReplyDelete
  4. I work and study. Time is constantly not enough. But it is necessary to write an essay. I found a great solution http://essaylab.co.uk/ You can order a service at any time of the day or night. Always on time. And the prices are excellent.

    ReplyDelete
  5. Working and studying. Time is constantly not enough. But you need to write an essay. I found a great solution http://essaylab.co.uk/ You can order the service at any time of the day or night. Always on time. And the prices are excellent.

    ReplyDelete

Back to Top