Trivial C, C++ Programs.

Fibonacci Series (Using Recursion) in C++



This is a simple 2 statement function that takes n as an argument and returns the nth Fibonacci number. Can be used to display the Fibonacci series upto the integer size limit.

 
#include <iostream.h>
#include <conio.h>
 
unsigned int fibonacci (unsigned int n)
{
  if (n <= 2)
    {
      return 1;
    }
  else
    {
      return fibonacci(n-1) + fibonacci(n-2);
    }
}
 
int main()
{
  unsigned int i, j=0;
 
  cout<<"\n Enter the fibonnaci number : ";
  cin>>i;
 
  for(j=1; j<=i; j++)
    cout<<fibonacci(j)<<" ";
 
  getch();
}

5 Comments to Fibonacci Series (Using Recursion) in C++

  1. tanmay's Gravatar tanmay
    January 22, 2010 at 11:04 | Permalink

    my search ended here .
    thanksssssssssssssss…….

  2. nalini singh's Gravatar nalini singh
    February 18, 2011 at 05:55 | Permalink

    how recursion prgm excuted …..plzzzz give an instruction…..

  3. mahya's Gravatar mahya
    April 28, 2011 at 02:45 | Permalink

    Thank you very, very big thanks to you I write it right for me to just start writing programs have had really hard

  4. Abhishek Agarwal's Gravatar Abhishek Agarwal
    July 10, 2011 at 01:51 | Permalink

    Nice. Thanks for the article. Also I found this article which discusses two methods to do the same. http://mytechrant.com/?p=50

  5. satinderpal singh's Gravatar satinderpal singh
    October 23, 2011 at 09:35 | Permalink

    your programe have very mistakes….
    please resolve your mistakes………..

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">