Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int fibonacci(int n) {
  5.     int f0 = 0;
  6.     int f1 = 1;
  7.     int fn = 1;
  8.     int i;
  9.  
  10.     if (n < 0) {
  11.         return -1;
  12.     } else if (n == 0 || n == 1) {
  13.         return n;
  14.     } else {
  15.         for (i = 2; i < n; i++) {
  16.             f0 = f1;
  17.             f1 = fn;
  18.             fn = f0 + f1;
  19.         }
  20.     }
  21.     return fn;
  22. }
  23.  
  24. int main() {
  25.     int i,n;
  26.     cout << "Nhap n:";
  27.     cin >> n;
  28.     for (i = 0; i < n; i++) {
  29.         cout << fibonacci(i) << " ";
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement