Pages

get our extension

Search program

Monday, 29 April 2013

Find N Fibonacci Series (Using Recursion) .....


package com.alok;
import java.util.Scanner;

class calc
{
int fibo(int n)
{
if(n==0)
return 0;
if(n==1)
return 1;
else
return fibo(n-1)+fibo(n-2);
}
}
public class Fibonacci
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter fibonacci Term :");
int n=sc.nextInt();
System.out.println("Fibonacci Series is :\n");
calc c=new calc();
for(int i=0;i<n;i++)
{
System.out.print("   "+c.fibo(i));
}
}
}

O/P:-


Enter fibonacci Term :
8
Fibonacci Series is :

   0   1   1   2   3   5   8   13


7 comments:

  1. sir,
    hoe to print a febonacci series in reverse order like
    8,5,3,2,1,1,0

    ReplyDelete
  2. write for(i=n;i>=0;i--)
    now result will print in recerse order

    ReplyDelete
    Replies
    1. Sir how to print reverse Fibonacci series without using recursion ??

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

      Delete
    3. int a=0,b=1;
      S.o.p (a,b);
      for(i=0;i<n-2;i++)
      {
      sum=a+b;
      S.o.p(sum);
      a=b;
      b=sum;
      }

      Delete
  3. Sir how to print reverse Fibonacci series without using recursion ??

    ReplyDelete
  4. Hi, this is good program as a learner. Try to implement using array to stored computed value, instead of vast number of recursive calls. Test your program to print count of 30, 50 or more numbers.

    ReplyDelete