Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- using namespace std;
- void fibonacci(int n, int& a, int& b) {
- if (n <= 0) return;
- cout << a << " ";
- int next = a + b;
- a = b;
- b = next;
- fibonacci(n - 1, a, b);
- }
- int main() {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- int n;
- while (true) {
- cout << "Введіть кількість членів ряду (від 1 до 40): ";
- cin >> n;
- if (cin.fail() || cin.peek() != '\n' || n < 1 || n > 40) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Кількість членів було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- int a = 0, b = 1;
- fibonacci(n, a, b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement