Facebook
From Aaa, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 111
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int fibonacci(int n) {
  5.     if (n <= 1)
  6.         return n;
  7.     int a = 0, b = 1;
  8.     for (int i = 2; i <= n; ++i) {
  9.         int temp = b;
  10.         b = a + b;
  11.         a = temp;
  12.     }
  13.     return b;
  14. }
  15.  
  16. int main() {
  17.     int num;
  18.     cout << "Enter the number of Fibonacci numbers to generate: ";
  19.     cin >> num;
  20.  
  21.     cout << "Fibonacci sequence:";
  22.     for (int i = 0; i < num; ++i) {
  23.         cout << " fib(" << i + 1 << ")=" << fibonacci(i) << " ";
  24.     }
  25.  
  26.     return 0;
  27. }