Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int fibonacci(int n) {
- int f0 = 0;
- int f1 = 1;
- int fn = 1;
- int i;
- if (n < 0) {
- return -1;
- }
- else if (n == 0 || n == 1) {
- return n;
- }
- else {
- for (i = 2; i < n; i++) {
- f0 = f1;
- f1 = fn;
- fn = f0 + f1;
- }
- }
- return fn;
- }
- int main() {
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) {
- cout << fibonacci(i) << " ";
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement