Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Fibonacci sequence is the sequence: 1, 1, 2, 3, 5, 8, . . . , such that the

ID: 3719353 • Letter: T

Question

The Fibonacci sequence is the sequence: 1, 1, 2, 3, 5, 8, . . . , such that the first and the second values are 1, and the value in any position n >= 3 is the sum of the values in the two previous positions: the value in position 3 is the sum of the values in positions 2 and 1; the value in position 4 is the sum of the values in positions 3 and 2, . . . etc. Using two integer variables previous1 and previous2 to hold the values in the two previous positions from the current position, and a variable new to hold the value in the current position, Write a C++ code segment to read an integer value greater than or equal to 3, and to output the Fibonacci sequence from position 3 to the position that corresponds to the value read. For example, if the input value is 8, the output will be: 2, 3, 5, 8, 13, 21.

Explanation / Answer

#include <iostream>

using namespace std;

int main(){

    int n1=0, n2=1;

    int sum=0;

    int terms,count=0;

    cout<<"Enter total temrs: ";

    cin>>terms;

    //display starting two numbers 0 1

    cout<<n1<<" "<<n2<<" ";

    while (count<(terms-2)){

        sum=n1+n2;    //sum of first and second number

        n1=n2; //assign second number in first variable

        n2=sum; //assign sum in second variable

        cout<<sum<<" "; //print sum

        count+=1;

    }

    cout<<endl;

    return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote