By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and
ID: 3857622 • Letter: B
Question
By definition, the first two numbers in the Fibonacci sequence are 0 and 1, and each subsequent number is the sum of the previous two;
Please use the code and don't change it:
#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>
using namespace std;
// YOUR CODE GOES HERE
int main()
{
int num;
int fibb;
cout << "Please enter a number between 5 and 22: ";
cin >> num;
cout << num << endl;
if ((num < 5) || (num > 22))
cout << "Please follow the directions!" << endl;
else {
fibb = fibonacci(num);
cout << endl;
cout << "Fibonacci # " << num << " is " << fibb << endl;
}
}
Explanation / Answer
Our Code will be:
int fibonacci (int n) // passing number value while calling function
{
int c, first=0, second=1, next=0;
cou<<"First"<<n<<"terms of Fibonacci series are:"<<endl;
for (c=0; c<n; c++)
{
if ( c <= 1)
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout<<next;
}
cout<<"Fibonacci #"<<n<<"is""<<first+second;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.