#include <iostream>
using namespace std;
int main() {
int n; // Number of terms of Fibonacci's sequence
int a = 1, b = 1, c; // Three consecutive Fibonacci's terms
cout << "Enter a no.: "; // Ask for a number
cin >> n; // Take input
if (n-- > 0) // If n is at least 1 then output 1st term
cout << a;
if (n-- > 0) // If n is at least 2 then output 2nd term as well
cout << " " << b;
while (n-- > 0) { // Continue outputting all next terms
c = a + b; // Compute the next term
cout << " " << c; // Output it
// Prepare for computing the next term:
a = b;
b = c;
}
cout << endl;
return 0;
}
http://www.AssignmentExpert.com/</iostream>
Comments