C++

Fibonacci sequence C++

The Fibonacci series/sequence is a series of numbers created when the next number is obtained by having a sum of the last two numbers in a series. The first two numbers are always 0 and 1. The Fibonacci series can be obtained in any programming language, but here we will apply the source code in the C++ programming language. In mathematics, the sequence of Fibonacci is explained through recursion relation having a sample formula.

Fn = Fn-1 + Fn-2

This tutorial will contain different methodologies to create a sequence of Fibonacci numbers.

Example 1

In this example, first, the library of input and output streams is used to enable the cin and cout streams, plus the user’s involvement is also encouraged through this library. Inside the main program, we will take two integer-type variables and declare them zero value. Another nextterm variable is also used that is initialized as zero and placed for later use. We will ask the user to enter a number he requires in the Fibonacci series. In other words, the number of lines displayed as output depends upon the user’s input. The specific number the user will enter, the result will contain the answer in these lines.

We need a ‘for’ loop to iterate up to that specific number that the user enters to calculate the sequence. This is a sort of limit by several lines. An if statement is used to check the number; if it is one, then display it as it is without any change. Similarly, the second number will be displayed likewise. In the Fibonacci sequence, the first two numbers are displayed. To proceed forward, we have used the continue statement. To calculate the series further, we will add both values. And this one will be the third number in the series. After that swapping process starts, the first variable will be assigned the value of the second variable, and the second variable will contain the third value stored in the nextterm variable.

Nextterm = t1 + t2;

T1 = t2;

T2 =nextterm;

Now each value is displayed separated by commas. Execute the code through a compiler. ‘-o’ is used to save the output of the code present in the input file.

$ g++ -o fib fib.c
$ ./fib

You can see that when the program is executed, the user will ask to enter the number he has entered 7, then the result will be of 7 lines, no matter at what point the Fibonacci series has reached the 7th point.

Example 2

This example will contain the calculation of the Fibonacci series by limiting the nextterm value. This means that the Fibonacci series can be customized by providing a specified number to which extent you want. Unlike the previous example, the result depends not on the number of lines but the number of series given by a number. We will start from the main program, the variables are the same, and the approach of the user involvement is the same as well. So the two first variables are initialized as zero at the start, the nextterm variable is declared zero. Then the user will enter the number. The first two terms are then displayed that are always 0 and 1.

The nextterm value will be assigned the value obtained by adding the numbers present in the first two variables. Here a while loop is used to apply a condition of creating the series until the value in the nextterm variable is equal to or less than the number that the user gives.

While (nextterm <= n)

Inside this while loop, the logic will be applied by swapping the numbers in the backward direction. The nextterm variable will again add the values of variables.

Nextterm = t1 + t2;

Now save the file and compile it to execute the code in the terminal.

When you run the code, the system will demand a number from you that must be a positive number. Then you will see that on calculation, a series of numbers till the 55th number is displayed.

Example 3

This source code that we will mention will contain a different method of calculating the Fibonacci series. Till now, we have calculated the series inside the main program. This example uses the involvement of a separate function to calculate this sequence of numbers. Inside the function, a recursive call is made to continue the process. Hence it is also an example of recursion. The function will take the number in the parameter, up to which the series is to be calculated. This number is sent from the main program. An if statement is used to check if the number is smaller than or equal to 1, then return the number itself because we need a minimum of two numbers to calculate the series. In the second case, when the condition becomes false and the number is greater than 1, calculate the series by repeatedly using a recursive call to the function itself.

Fib (n-1) + fib (n-2);

This shows that in the first part, one number before the total number is passed to the function, that value will be subtracted from the number obtained from the cell that contains two numbers previous to the total number as a parameter.

Now in the main program, the number is assigned to the variable, and the first function call is made to pass the number to the function. Now execute the file’s source code in the terminal to get the answer. Here you will see that ’13’ is the answer, as the number entered was 7, so the series will be 0+1+1+2+3+5+8+13.

Example 4

This example involves the OOP (object-oriented programming) approach of calculating the Fibonacci series. A class GFG is created. In its public part, a function is created to have an array that will store the Fibonacci series.

F [n +2];

Here n is the number declared as 0 at the start.

F[0] = 0;

F[1] = 1;

The numbers at index 0 and 1 are declared as 0 and 1.

After that, a ‘for’ loop is used in which the Fibonacci series will be calculated. The two previous numbers are added to the series and will be stored.

F[i] = f[i-1] + f[i -2];

After that, the specific number at a particular index is returned.

A function call is made by using the object.

g.fib(n);

Now execute the code, and you will see that as the number is 11, the sequence will be till the 11th digit.

Conclusion

This article ‘Fibonacci sequence C++’ is a mixture of different approaches used to create a sequence by adding the two previous numbers. With a simple swapping technique, in addition to the recursion method and with the help of arrays, we can generate these numbers in series. To create the Fibonacci series, the numbers are recommended to be in integer data type. We can calculate the series by applying limitations on the number of lines and the number of the sequence.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.