C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,293 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,130 people online right now. Registration is fast and FREE... Join Now!





Recursive Fibonacci Sequence

This code shows how to generate a Fibonacci Sequence using Recursion.

Submitted By: born2c0de
Actions:
Rating:
Views: 65,699

Language: C++

Last Modified: March 2, 2005

Snippet


  1. long fib(unsigned long n) {
  2.     if (n <= 1) {
  3.         return n;
  4.     } else {
  5.         return fib(n-1)+fib(n-2);
  6.     }
  7. }

Copy & Paste


Comments


mahir 2007-11-15 07:47:24

where is header file

born2c0de 2008-01-18 04:00:07

This function does not require a header file. Besides, it is a code snippet and not a complete application. You will have to call it from main() or any other function to use it.

giotto 2008-02-04 08:59:49

Oh, come on! It can be done, in one line! int fibonacci(int number){ return number

giotto 2008-02-04 09:00:20

int fibonacci(int number){ return number

giotto 2008-02-04 09:00:49

:S

nurboja 2008-05-03 03:07:39

int fib(int x) { if (x==0) return 0; else if (x==1) return 1; else return fib(x-1)+fib(x-2); }

nurboja 2008-05-03 03:08:04

Fn=0 za n= 0; Fn=1 za n= 1; Fn=Fn–1+ Fn–2 za n>=2;

yuyu89 2009-01-26 05:37:14

what is the header to run this codes int fib(int x) { if (x==0) return 0; else if (x==1) return 1; else return fib(x-1)+fib(x-2); }

shabaaz 2009-04-15 12:47:40

also use "static int"for f1 and f2

shabaaz 2009-04-15 12:48:29

regular c header.....stdio.h or iostream for c++

shida 2009-07-29 03:05:59

how can I write fibonacci with arrays?

autumn_storm2216 2009-08-07 04:29:40

what if x = 2 which in fibonacci is also 1.you just give the condition if x = 1?


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month