See the description of the definition of the Fibonacci number sequence on the fo
ID: 3642296 • Letter: S
Question
See the description of the definition of the Fibonacci number sequence on the following link: http://en.wikipedia.org/wiki/Fibonacci_numberYou only need to read the first three paragraphs.
You are to write a program that asks the use to enter an integer n and then print out the Fibonacci sequence that ends with the number Fn. The integer n must be greater than or equal to zero.Your program must have a function that calculates and prints the Fibonacci sequence.
The output should look exactly like what is after the colon. Rows, commas, (...) should all be in the code, except the words input/output.
Input: 0
Output: Which Fibonacci number do you want to find?
0...
Input: 1
Output: Which Fibonacci number do you want to find?
0, 1...
Input: 2
Output: Which Fibonacci number do you want to find?
0, 1, 1...
Input: -1, -100, 3
Output: Which Fibonacci number do you want to find?
The number entered must be greater than or equal to zero
Which Fibonacci number do you want to find?
The number entered must be greater than or equal to zero
Which Fibonacci number do you want to find?
0, 1, 1, 2...
Input: 10
Output: Which Fibonacci number do you want to find?
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55...
Input: 11
Output: Which Fibonacci number do you want to find?
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...
Input: 12
Output: Which Fibonacci number do you want to find?
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...
Input: 13
Output: Which Fibonacci number do you want to find?
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233...
This is what I have so far:
#include <iostream>
using namespace std;
int fibo(unsigned int n)
{
//n_minus_1 represents what's supposed to be F(n-1)
//n_minus_2 represents what's supposed to be F(n-2)
//sum represents F(n) which equals F(n-1)+F(n-2)
double n_minus_1=0,n_minus_2=1,sum;
switch(n)
{
//if n=0 then F(n)=0
case 0:
return 0;
//if n=1 then F(n)=1
case 1:
return 1;
//otherwise the sum (i.e F(n)) is equal to (n_minus_1)+(n_minus_2) (i.e F(n-1)+F(n-2)
//the counter keeps on moving the variables areound 'n' times
//so that on each time F(n-2) becomes equal to F(n-1) and F(n-1) becomes equal to F(n)
//and F(n) is reevaluated each time in the loop
default:
for(int counter=0;counter<n;counter++)
{
sum=n_minus_1+n_minus_2;
n_minus_2=n_minus_1;
n_minus_1=sum ;
}
}
return sum;
}
int main()
{
int n;
int i=0;
cout<<"Which Fibonacci number do you want to find? ";
cin>>n;
if (n<0) cout<<"0";
while (i<=n)
{
cout<<fibo(i) << "... ";
++i;
}
cin.sync();
cin.get();
}