Advertisement
LisunovaMaryna

lab1.2 c++

Sep 26th, 2023 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int a, b, n = 0;
  8.     a = 1;
  9.     b = 1;
  10.     cout << "The program will calculate the n-th term of the Fibonacci series.\n";
  11.     bool IsCorrect;
  12.     cout << "Enter the Fibonacci series member number: ";
  13.     do
  14.     {
  15.         IsCorrect = false;
  16.         cin >> n;
  17.         if ((cin.fail()) or (n < 0) or (n == 0))
  18.         {
  19.             IsCorrect = true;
  20.             cout << "Incorrect value. Enter the number: ";
  21.         }
  22.         cin.clear();
  23.         while (cin.get() != '\n');
  24.     } while (IsCorrect);
  25.     for (int i = 2; i < n; i++)
  26.     {
  27.         b = a + b;
  28.         a = b - a;
  29.     }
  30.     cout << b;
  31.  
  32.     return 0;
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement