Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

a term index is entered and Fibonacci sequence is calculated up to that term.

If the index is 5 then the sequence is : 0, 1, 1, 2, 3

if the index is 7 then the sequence is : 0, 1, 1, 2, 3, 5, 8

Write a program that will print the Fibonacci sequence to the term index entered by the user.

The program will ask you if you want to print another sequence; if Yes repeat if No end your program

Respuesta :

S0unds

//C++:

#include <iostream>

#include <string.h>

using namespace std;

int F(int n) {

 if(n == 0) {

   return 0;

 }

 if(n == 1) {

   return 1;

 }

 else  

   return F(n-1) + F(n-2);

 }

int main()

{

   int index;

   int ok=0;

   char x[3];

   do{

   cout<<"index=";cin>>index;

   for(int i=0;i<index;i++)

   cout<<F(i)<<' ';

   cout<<endl;

   cout<<"Repeat? (Yes/No):";

   cin>>x;

   if(strcmp(x,"No")==0)

   ok=1;

   cout<<endl;

   }while(ok==0);

   return 0;

}

ACCESS MORE