Advertisement
paulogp

Fibonacci

Sep 19th, 2011
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. // msvc++
  2. // paulogp
  3. #include <iostream> // cout...
  4. #include <Windows.h> // Sleep
  5.  
  6. using namespace std;
  7.  
  8. unsigned fibonacci (unsigned the_n);
  9.  
  10. unsigned fibonacci (unsigned the_n)
  11. {
  12.     unsigned the_sum = 0;
  13.  
  14.     if (the_n > 0)
  15.     {
  16.     for (unsigned i = 0, f0 = 0, f1 = 1; i < (the_n - 1); ++i)
  17.     {
  18.         the_sum = f0 + f1;
  19.         f0 = f1;
  20.         f1 = the_sum;
  21.     }}
  22.  
  23.     if (the_n > 1)
  24.         return the_sum;
  25.     else
  26.         return the_n;
  27. }
  28.  
  29. int main()
  30. {
  31.     unsigned the_n;
  32.  
  33.     cout << "number: ";
  34.     cin >> the_n;
  35.  
  36.     cout << "f(" << the_n << "): " << fibonacci (the_n) << endl;
  37.  
  38.     Sleep (5000);
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement