garryhtreez

6.37

Dec 16th, 2020
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. /* 6.37 Iterative fibonacci function
  2.    Deitel & Deitel C++ How to Program, 10th ed (Indian subcontinent adaptation)
  3.    Visual Studio Community 2019
  4. */
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. unsigned long long fibonacci( int num ) {
  9.    unsigned long long fib{ 0 }, fib1{ 1 }, fib2{ 0 };
  10.    int i{ 0 };
  11.    if ( ( num == 0) || (num == 1 ) ) {
  12.       return num;
  13.    }
  14.    for ( i = 2; i <= num; i++ ) {
  15.       fib = fib1 + fib2;
  16.       fib2 = fib1;
  17.       fib1 = fib;
  18.    }
  19.    return fib;
  20. }
  21.  
  22. int main() {
  23.    for ( int count{ 0 }; count <= 55; count++ )
  24.       cout << "fibonacci(" << count << ") = " << fibonacci(count) << endl;
  25. }
Advertisement
Add Comment
Please, Sign In to add comment