Mira2706

hw1_3

Nov 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int first = 0;
  8.     int second = 1;
  9.     int next = 0;
  10.     int n;
  11.  
  12.     cout<<"Enter a number as close as possible to the Fibonacci series: ";
  13.     cin>>n;
  14.  
  15.  
  16.  
  17.    do
  18.    {
  19.     next = first + second;
  20.  
  21.     first = second;
  22.     second= next;
  23.  
  24.    }
  25.  
  26.    while(next<n);
  27.  
  28.  
  29.     if (abs(n-first)<abs(n-next))
  30.     {
  31.         cout<<first<<endl;
  32.     }
  33.     else
  34.     {
  35.         cout<<next<<endl;
  36.     }
  37.  
  38.  
  39.  
  40.  
  41.     return 0;
  42. }
Add Comment
Please, Sign In to add comment