Fibonacci sequence C++ - ProgrammerTech
Language:

Search

# Fibonacci sequence C++

## Fibonacci Series

The Fibonacci Sequence is a series of numbers in which any number in it is equal to the sum of the previous two numbers. That is, the first and second numbers are equal to the third, and it was named after the Italian mathematician Leonardo Fibonacci.

fibonacci numbers list

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

In the previous numbers we notice fibonacci numbers if we add the numbers 0 and 1 equals 1. And if we want to choose the number 8 we notice the sum of the two numbers before it 5 and 3 equals 8. This is the concept of the sequence in short (meaning the third number is equal to the sum of the two numbers preceding it).

## Examples of Fibonacci sequences in C++

example 1

Write a program that calculates the sequence of numbers 1 1 2 3 5 8 from the fibonacci numbers list.

``````#include <iostream>
using namespace std;
main()
{
int n, c, first = 0, second = 1, next;
cout << "Enter the number of terms of fibonacci series ou want" << endl;
cin >> n;
cout << "First" << n << "terms of fibonacci series are : " << endl;
for (c = 0 ; c < n ; c++)
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
return 0;
}``````

We create two integer variables c and n and then show the user a message asking them to enter a number for the length of the sequence. And by cin the user will enter the number into the program. Then, after executing the program, it will arrange the data of the sequence for us. It will write to us Firts, i.e. the beginning of the sequence, then write to us the value of n, which is the beginning of the Fibonacci sequence, and then write that the Fibonacci sequence is and then print the numbers of the sequence. After that, we enter an iterative loop in order to arrange and arrange the printing of the sequence.

When we run the code

``````0
1
1
2
3
5
8``````

Example 2

Write a Fibonacci Sequence program that prints up to n infinity of terms.

``````#include <iostream>
using namespace std;
int main()
{
int n, t1 = 0, t2 = 1, nextTerm = 0;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 1; i <= n; ++i)
{
if(i == 1)
{
cout << " " << t1;
continue;
}
if(i == 2)
{
cout << t2 << " ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout << nextTerm << " ";
}
return 0;
}``````

When we run the code and write the number 10, for example, the result will be.

``````Enter the number of terms: 10
Fibonacci Series: 01 1 2 3 5 8 13 21 34``````

example 3

``````#include <iostream>
using namespace std;
int main() {
int n1=0,n2=1,n3,i,number;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" ";
for(i=2;i<number;++i)
{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
return 0;
}``````

When we run the code and write the number 15, for example, the result will be.

``````Enter the number of elements: 15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377``````

Example 4

``````#include <iostream>
using namespace std;
void printFibonacci(int n){
static int n1=0, n2=1, n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
cout<<n3<<" ";
printFibonacci(n-1);
}
}
int main(){
int n;
cout<<"Enter the number of elements: ";
cin>>n;
cout<<"Fibonacci Series: ";
cout<<"0 "<<"1 ";
printFibonacci(n-2);
return 0;
}``````

When we run the code and write the number 20, for example, the result will be.

``````Enter the number of elements: 20
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181``````