## Java Certification Training Course

- 47k Enrolled Learners
- Weekend
- Live Class

C Programming and Data Structures (18 Blogs)

The Fibonacci Sequence is a peculiar series of numbers named after Italian mathematician, known as Fibonacci. Starting with 0 and 1, each new number in the Fibonacci Series is simply the sum of the two before it. For example, starting with 0 and 1, the first 5 numbers in the sequence would be 0, 1, 1, 2, 3 and so on. In this article, let’s learn how to write the Fibonacci Series in C++.

Following Pointers will be covered in this article,

- What is a Fibonacci Series?
- C++ Program to generate Fibonacci Series till a number entered by user
- C++ Program to generate Fibonacci Series using Recursion

Fibonacci series is a series of numbers formed by the addition of the preceding two numbers in the series. The first two terms are zero and one respectively. The terms after this are generated by simply adding the previous two terms.

**Here is an example** of Fibonacci series: 0,1,1,2,3,5,8,13….etc.

In the above example, 0 and 1 are the first two terms of the series. These two terms are printed directly. The third term is made by adding the first two terms. In this case 0 and 1. So, we get 0+1=1. Hence 1 is printed as the third term. The next term is generated by using the second and third term and not using the first term. It is done until the number of terms requested by the user. In the above example, we have used eight terms.

#include<iostream> using namespace std; int main() { int first=0, second=1, i, n, sum=0; cout<<"Enter the number of terms: "; cin>>n; //accepting the terms cout<<"Fibonacci Series: "; for(i=0 ; i<n ; i++) { if(i <= 1) { sum=i; } // to print 0 and 1 else { sum=first + second; first=second; second=sum; //to calculate the remaining terms. //value of first and second changes as new term is printed. } cout<<sum<<" "; } return 0; }

In the above program, we first declare all variables. First, we set the values for first and second, these will be the variables we will use to generate further terms. Next, we declare the term n, that will hold the number of terms. We have a term to hold the sum of the two digits called sum. The last term is i. It is used for iteration in the for loop.

We accept the number of terms from the user and store it in n. We then have a for loop that runs from 0 to the number of terms requested by the user, that is n.

Inside the for loop, we first have an if statement with the condition checking if the value of i if it is less than 1. If it is zero or one is printed, depending on the number of terms. It is used to print the initial zero and one when there are more than two terms.

If the number of terms is greater than one, the else part of the loop is executed. In this part, the addition of the variable first and second is assigned to the variable sum. The next term is the sum variable. For example, first and second whose values are 0 and 1 are added to get the sum value as 1.

In the next part, we assign the value of the second term to the first term and after that, the value of sum to the second term. This is done because for the next term the previous two values are changed as a new value is printed. This is the sum value. If we consider 0 and 1 assigned to first and second, after this step the value of first will be 1 and the value of the second will also be 1 because the value of sum is 1.

After exiting the else part, we print the sum value. This is executed until the value of i becomes equal to n. The loop breaks and we exit the program.

Moving on with this article on Fibonacci Series in C++

**Code:**

#include<iostream> using namespace std; int main() { int first = 0, second = 1, sum = 0, n; cout<<"Enter the end term for the series: "; cin>>n; cout<<"Fibonacci Series: "<<first<<" "<<second; sum = first + second; while(sum <= n) { cout<<sum<<" "; first = second; second = sum; sum = first + second; } return 0; }

**Output:**

In this program, we take the end term from the user. We must display a Fibonacci series up to that number. This is done by using a while loop.

We take input from the user which is the last term. Then print the first and second terms. After this, add first and second and store it in sum.

Then, there is a while loop. It runs till the value of the sum is less than that of the number entered by the user. Inside the while loop, Print out the sum first.

In the next part, we assign the value of the second term to the first term and after that, the value of sum to the second term. We perform addition again adding first and second term and assigning it to sum.

The loop runs till the sum value is greater than the number entered by the user.

Moving on with this article on Fibonacci Series in C++, let’s write a C++ program to print Fibonacci series using recursion.

Another way to program the Fibonacci series generation is by using recursion.

#include<iostream> using namespace std; int fibonacci(int); int main() { int n, m= 0, i; cout<<"Enter the number of terms: "; cin>>n; //accepting the terms cout<<"Fibonacci Series: "; for (i = 1; i <= n; i++) { cout<<" "<<fibonacci(m); m++; } return 0; } int fibonacci(int n) { if (n == 0 || n == 1) return n; else return (fibonacci(n-1) + fibonacci(n-2)); }

**Output:**

In this program, we use recursion to generate the Fibonacci series. The function Fibonacci is called recursively until we get the output.

In the function, we first check if the number n is zero or one. If yes, we return the value of n. If not, we recursively call Fibonacci with the values n-1 and n-2.

**These are the ways of generating a Fibonacci series. With this, we have come to the end of this article. **

*Got a question for us? Please mention it in the comments section of this “Fibonacci Series in C++” blog and we will get back to you as soon as possible.*

Comments 0 Comments

REGISTER FOR FREE WEBINAR

Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP