HomeLinuxFibonacci Sequence in C++

Fibonacci Sequence in C++


The Fibonacci collection is a set of numbers that’s produced by including the 2 earlier values within the sequence. The primary and second parts are 0 and 1, respectively. By merely combining the primary two phrases, the subsequent phrases are produced.

Producing the Fibonacci Sequence Utilizing the For-Loop

For our first illustration, we generate a Fibonacci collection utilizing the for-loop in C++. We declare the variables. Then, the primary and final variable’s values are set that are used to create the subsequent phrases. The size of parts within the Fibonacci collection are taken from the person. Utilizing the for-loop, we generate the phrases as much as the user-provided variety of parts.

#embody <iostream>  
utilizing namespace std;  

int fundamental()
{  
  int v1=0,v2=1,v3,j,n;    
 cout<<“Please Enter the variety of parts you need within the Fibonacci sequence: “;    
 cin>>n;    
 cout<<“The fibonacci collection: “<<v1<<” “<<v2<<” “;
 for(j=2;j<n;++j) //loop begins from 2 as a result of 0 and 1 are already printed    
 {    
  v3=v1+v2;    
  cout<<v3<<” “;    
  v1=v2;    
  v2=v3;    
 }    
   return 0;  
 }

For the profitable compilation of this system, we have to add the required libraries. We included the “iostream” library. The usual “namespace” library can also be used.

The principle code begins with invoking the principle() perform. The physique of the principle() perform begins with the curly brackets. We declare some variables with the int datatype. The “v1” variable incorporates the primary worth and is about to 0. The second variable which is “v2” is about to 1. It is because the Fibonacci collection all the time have the primary two parts as 0 and 1. The “v3” variable holds the worth that’s obtained from the sum of “v1” and “v2”. The “j” variable is used for iteration within the for-loop, whereas the “n” variable holds the variety of phrases which we take from the person.

After declaring all of the variables, we use the cout output object to print the “Please Enter the variety of parts you need within the Fibonacci sequence:” textual content on the console. The cin enter object is used to take the variety of parts and retailer them within the “n” variable.

One other cout assertion is then used to print the values of the primary and second variables – “v1” and “v2” – because the Fibonacci collection begins with these two parts and additional parts are generated with them.

The for-loop is initialized within the code. We use the for-loop when a sure block of code must be executed for the required variety of occasions. The situation contained in the loop is outlined which says “for( j=2; j<n; ++j)”. Right here, the loop begins with the worth of “j=2” variable. The loop continues the iteration till the worth of “j” is lower than the user-defined worth saved in “n”.

The person gives the variety of parts as 6.

The loop begins by taking the preliminary worth of “j=2”. Then, it checks if the worth of “j” is lower than the worth of “n” which is 2 lower than 6. If the situation turns into true, the management executes the physique of the loop. Within the physique, we generate the sum of “v1” and “v2” which is “0+1” and assign the outcome which is “2” to “v3”. Now, the brand new worth is saved in “v3”. The present worth of “v3” is printed utilizing the cout assertion.

Within the subsequent line, we assign the worth of the second time period “v2” to the primary time period “v1”. Then, the sum that’s saved in “v3” is assigned to “v2”. Now, the up to date worth of “v1” is 1 and the up to date worth for “v2” is 2. There aren’t any extra statements to execute so the management strikes to the subsequent step of the loop. The worth of “j” is elevated and is transformed to three.

The loop begins once more, this time with “j” as 3. The situation is happy since 3 is lower than 6. Once more, the management strikes into the physique of the for-loop and repeats the beforehand talked about steps with the up to date values of “v1” and “v2”.

In the identical method, the loop continues iterating till the worth of “j” will get higher or equal worth to the “n”. As quickly because the situations turn out to be false, the loop breaks and executes the operation that’s written within the code. Then, this system terminates.

The output picture reveals that the Fibonacci collection is generated from the previously-explained program:

Producing the Fibonacci Sequence Utilizing the Whereas-Loop

On this instance, we are going to study to generate a Fibonacci sequence utilizing the while-loop.

#embody<iostream>
utilizing namespace std;
int fundamental()
{
    int n1 = 0, n2 = 1, s = 0, num;
    cout<<“Enter the final time period for the Fibonacci collection: “;
    cin>>num;
     cout<<“That is required Fibonacci Sequence: “<<n1<<”  “<< n2;
   
     s = n1 + n2;
     whereas(s <= num)
     {
        cout<<s<<” “;
        n1 = n2;
        n2 = s;
        s = n1 + n2;
    }
    return 0;
}

The preliminary a part of the code incorporates the wanted libraries. Then, the principle() perform begins. We first declare the integer variables that we are going to later use within the code. We’ve got the “n1” variable initialized to 0, the “n2” variable set to 1, the sum variable “s” initially set to 0, and a “num” variable which takes the enter worth from the person concerning the final time period of the Fibonacci sequence.

After this, we print an “Enter the final time period for the Fibonacci collection:” assertion utilizing the cout object. Then, the worth is taken from the person and saved within the “num” variable with the cin object.

Once more, we make use of the cout object to output the values of the “n1” and “n2” variables. Within the subsequent step, the sum of “n1” and “n2” is calculated and assigned to the “s” variable. The while-loop is initialized which continues the looping till the worth of “s” is much less and equal to the worth of “num”. First, the worth of “s” is printed. Subsequent, we assign the worth of “n2” to “n1” and the worth of “s” to “n2”. The sum of the up to date values of “n1” and “n2” is calculated and saved within the “s” variable.

The management strikes again to the loop and the situation is checked with the up to date worth of “s”. The up to date worth of “s” is printed with each looping which creates the Fibonacci sequence. When the situation turns into false and the loop breaks, this system terminates by executing the remaining operations of the code.

The picture reveals the final time period that’s entered by the person:

The Fibonacci collection is generated if the user-specified time period is printed on the console.

Conclusion

The technology of a Fibonacci sequence in C++ is a crucial studying method. The Fibonacci collection might be created in a number of methods. On this matter, we demonstrated the 2 strategies to create a Fibonacci collection. The primary technique that we applied is utilizing the for-loop to generate the Fibonacci collection. Then, the creation of the Fibonacci collection with the while-loop is defined. Each these methods are absolutely virtually implementable.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments