Advertisement
Lavig

Другий семестр. Лабораторна робота №7-8 (Завдання 4)

Mar 15th, 2025
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. using namespace std;
  5.  
  6. void fibonacci(int n, int& a, int& b) {
  7.     if (n <= 0) return;
  8.     cout << a << " ";
  9.     int next = a + b;
  10.     a = b;
  11.     b = next;
  12.     fibonacci(n - 1, a, b);
  13. }
  14. int main() {
  15.     SetConsoleOutputCP(1251);
  16.     SetConsoleCP(1251);
  17.     int n;
  18.     while (true) {
  19.         cout << "Введіть кількість членів ряду (від 1 до 40): ";
  20.         cin >> n;
  21.         if (cin.fail() || cin.peek() != '\n' || n < 1 || n > 40) {
  22.             cin.clear();
  23.             cin.ignore(32767, '\n');
  24.             cout << "Кількість членів було введено неправильно. Спробуйте ще раз!" << endl;
  25.             continue;
  26.         }
  27.         else {
  28.             break;
  29.         }
  30.     }
  31.     int a = 0, b = 1;
  32.     fibonacci(n, a, b);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement