Generating the Fibonacci Series Using the For-Loop
For our first illustration, we generate a Fibonacci series using the for-loop in C++. We declare the variables. Then, the first and last variable’s values are set which are used to create the next terms. The length of elements in the Fibonacci series are taken from the user. Using the for-loop, we generate the terms up to the user-provided number of elements.
using namespace std;
int main()
{
int v1=0,v2=1,v3,j,n;
cout<<"Please Enter the number of elements you want in the Fibonacci sequence: ";
cin>>n;
cout<<"The fibonacci series: "<<v1<<" "<<v2<<" ";
for(j=2;j<n;++j) //loop starts from 2 because 0 and 1 are already printed
{
v3=v1+v2;
cout<<v3<<" ";
v1=v2;
v2=v3;
}
return 0;
}
For the successful compilation of the program, we need to add the required libraries. We included the “iostream” library. The standard “namespace” library is also used.
The main code starts with invoking the main() function. The body of the main() function starts with the curly brackets. We declare some variables with the int datatype. The “v1” variable contains the first value and is set to 0. The second variable which is “v2” is set to 1. This is because the Fibonacci series always have the first two elements as 0 and 1. The “v3” variable holds the value that is obtained from the sum of “v1” and “v2”. The “j” variable is used for iteration in the for-loop, whereas the “n” variable holds the number of terms which we take from the user.
After declaring all the variables, we use the cout output object to print the “Please Enter the number of elements you want in the Fibonacci sequence:” text on the console. The cin input object is used to take the number of elements and store them in the “n” variable.
Another cout statement is then used to print the values of the first and second variables – “v1” and “v2” – as the Fibonacci series starts with these two elements and further elements are generated with them.
The for-loop is initialized in the code. We use the for-loop when a certain block of code needs to be executed for the specified number of times. The condition inside the loop is defined which says “for( j=2; j<n; ++j)”. Here, the loop starts with the value of “j=2” variable. The loop continues the iteration until the value of “j” is less than the user-defined value stored in “n”.
The user provides the number of elements as 6.
The loop starts by taking the initial value of “j=2”. Then, it checks if the value of “j” is less than the value of “n” which is 2 less than 6. If the condition becomes true, the control executes the body of the loop. In the body, we generate the sum of “v1” and “v2” which is “0+1” and assign the result which is “2” to “v3”. Now, the new value is stored in “v3”. The current value of “v3” is printed using the cout statement.
In the next line, we assign the value of the second term “v2” to the first term “v1”. Then, the sum that is stored in “v3” is assigned to “v2”. Now, the updated value of “v1” is 1 and the updated value for “v2” is 2. There are no more statements to execute so the control moves to the next step of the loop. The value of “j” is increased and is converted to 3.
The loop starts again, this time with “j” as 3. The condition is satisfied since 3 is less than 6. Again, the control moves into the body of the for-loop and repeats the previously mentioned steps with the updated values of “v1” and “v2”.
In the same manner, the loop continues iterating until the value of “j” gets greater or equal value to the “n”. As soon as the conditions become false, the loop breaks and executes the operation that is written in the code. Then, the program terminates.
The output image shows that the Fibonacci series is generated from the previously-explained program:
Generating the Fibonacci Series Using the While-Loop
In this example, we will learn to generate a Fibonacci sequence using the while-loop.
using namespace std;
int main()
{
int n1 = 0, n2 = 1, s = 0, num;
cout<<"Enter the last term for the Fibonacci series: ";
cin>>num;
cout<<"This is required Fibonacci Series: "<<n1<<" "<< n2;
s = n1 + n2;
while(s <= num)
{
cout<<s<<" ";
n1 = n2;
n2 = s;
s = n1 + n2;
}
return 0;
}
The initial part of the code contains the needed libraries. Then, the main() function begins. We first declare the integer variables that we will later use in the code. We have 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 input value from the user about the last term of the Fibonacci sequence.
After this, we print an “Enter the last term for the Fibonacci series:” statement using the cout object. Then, the value is taken from the user and stored in the “num” variable with the cin object.
Again, we employ the cout object to output the values of the “n1” and “n2” variables. In the next step, the sum of “n1” and “n2” is calculated and assigned to the “s” variable. The while-loop is initialized which continues the looping until the value of “s” is less and equal to the value of “num”. First, the value of “s” is printed. Next, we assign the value of “n2” to “n1” and the value of “s” to “n2”. The sum of the updated values of “n1” and “n2” is calculated and stored in the “s” variable.
The control moves back to the loop and the condition is checked with the updated value of “s”. The updated value of “s” is printed with every looping which creates the Fibonacci sequence. When the condition becomes false and the loop breaks, the program terminates by executing the remaining operations of the code.
The image shows the last term that is entered by the user:
The Fibonacci series is generated if the user-specified term is printed on the console.
Conclusion
The generation of a Fibonacci sequence in C++ is an important learning technique. The Fibonacci series can be created in several ways. In this topic, we demonstrated the two methods to create a Fibonacci series. The first method that we implemented is using the for-loop to generate the Fibonacci series. Then, the creation of the Fibonacci series with the while-loop is explained. Both these strategies are fully practically implementable.