The next element in the Fibonacci Series in C++ is the sum of the previous two elements. The *Fibonacci Series* is a number sequence in which a number is found by adding the two numbers preceding it.

Beginning with 0 and 1, the sequence is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. It is named after Leonardo Fibonacci, an Italian mathematician who lived in the early thirteenth century.

Fibonacci numbers are a series in which each number is the sum of the two numbers before it.

The first two numbers in the Fibonacci sequence are either 1 and 1 or 0 and 1, depending on the sequence’s chosen starting point, and each subsequent number is the sum of the previous two.

In this article, we will discuss what the **Fibonacci Series** is, how to generate the **Fibonacci Series** until a user enters a number, and how to generate the “* Fibonacci Series“* using Recursion.

The expression for **Fibonacci Series** is, Xn= Xn-1+ Xn-2.

#include<iostream>

using namespace std;

int main()

{

int n, i, a=0, b=1, c;

cout << “Enter the number of terms: “;

cin >> n;

cout << “Fibonacci Series is: ” << endl;

for (i=a; i<=n; i++)

{

cout << a << ” “;

c=a+b;

a=b;

b=c;

}

return 0; }

**Output**

Enter the number of terms: 10

Fibonacci Series is:

0 1 1 2 3 5 8 13 21 34 55

In the preceding program, we declare all variables first. First, we set the values for the first and second variables, which will be used to generate additional terms.

Following that, we declare the term n, which will hold the number of terms. The sum is a term used to represent the sum of two digits.

The final term is i. It is used in the for loop to iterate. We accept the user’s term count and store it in n.

The for loop then runs from 0 to the number of terms requested by the user, n.

Within the for loop, we first have an if statement that checks if the value of, I is less than 1.

Depending on the number of terms, it is either zero or one. When there are more than two terms, it is used to print the initial zero and one.

If the number of terms is greater than one, the loop’s else section is executed.

The variable sum is assigned the addition of the variables first and second in this section. The sum variable comes next.

In the subsequent sections, we assign the value of the second term to the first term, followed by the value of the sum to the second term.

This is done because the previous two values are changed as a new value is printed for the next term. This is the total value.

If we consider the values 0 and 1 assigned to first and second, the value of first will be 1 after this step, and the value of second will also be 1 because the sum is 1.

We print the sum value after exiting the else part. This is repeated until the value of I equals n.

The loop is broken, and the program is terminated.

### Generating Fibonacci Series for the given number of a user

Consider the following example

include<iostream>

using namespace std;

int main()

{

int n, a=0, b=1, c;

cout << “Enter Range: “;

cin >> n;

cout << “Fibonacci Series is: ” << endl;

while ( a <= n )

{

cout << a << ” “;

c=a+b;

a=b;

b=c;

} return 0;

}

**Output**

Enter Range: 100

Fibonacci Series is:

0 1 1 2 3 5 8 13 21 34 55 89

#### Code Breakup

We use the user’s end term in this example. We must print a Fibonacci sequence up to that number.

A while loop is used to accomplish this. We take user input, which is the final term.

The first and second terms should then be printed. After that, add the first and second and store the result in sum.

Then comes a while loop. It runs until the sum’s value is less than the number entered by the user. Print out the sum first inside the while loop.

In the following section, we assign the value of the second term to the first term, followed by the value of the sum to the second term.

We repeat addition by adding the first and second terms and assigning them to a sum.

The while loop continues until the sum value exceeds the number entered by the user.

### Generating Fibonacci Series in C++ using Recursion

The recursive function/method divides the complex problem into identical single simple cases that can be easily handled.

Divide and conquer is a well-known computer programming technique.

The fundamental case for determining factorial fibonacci(0) = 0 & fibonacci(1) = 1.

In general, factorial will be found by fibonacci(n) = fibonacci(n-1) + fibonacci (n-2).

#include<iostream>

using namespace std;

int fibonacci(int);

int main()

{

int n;

cout << “Enter term: “;

cin >> n;

cout << “Fibonacci term= ” << fibonacci(n) << endl;

return 0;

}

int fibonacci(int n)

{

if(n <= 1)

return n;

else

return (fibonacci(n-1) + fibonacci(n-2) );

}

**Output**

Enter term: 7

Fibonacci term = 13

Enter term: 10

Fibonacci term = 55

Enter term: 15

Fibonacci term = 610

The Fibonacci series is generated using recursion in this program. The function Fibonacci is called recursively until the output is obtained.

In the function, we first determine whether the number n is zero or one.

If this is the case, we return the value of n. If not, we call Fibonacci with the values n-1 and n-2 recursively.

**Conclusion**

Develop your programming skills by joining our Java Training in Chennai at Softlogic Systems.

We have wonderful curriculum, expert and experienced trainers, and project practices for providing the best coaching online mode as well as classroom mode.