Question #47569

5. Write a program that generates and displays the Fibonacci sequence numbers of n(as input). In fibonacci, the current third number is the sum of two previous numbers;
Sample input/output dialogue:
Enter a no.: 9
Fibonacci series: 11 2 3 5 8 13 21 34
1

Expert's answer

2014-10-10T02:05:40-0400
#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>

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS