Program to print the Fibonacci Series in Python - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Sunday, 20 December 2020

Program to print the Fibonacci Series in Python


Hey all, in this amazing tutorial, we are going to write a program to print the Fibonacci Series in Python. So get ready with your code editors as we are going to do this program in the best way possible!


As always, you can find the final code at the end of this tutorial. But I would suggest you to be with us this whole time as we are going to go through each and every step you need to take to become the best programmer in the world.


Excited? Let's start with the some basics.


What is a Fibonacci Series?


The Fibonacci Series is a collection of numbers starting from 0 and 1, where each number is the sum of the previous two numbers. Understood? Maybe not.


Here's an example: The Fibonacci Series of n terms is given as:

0, 1, 1, 2, 3 ...... till n terms 


If you want to find the nth term of a Fibonacci Sequence in Python, you should check our previous article. Please read that as there we have covered the basics of the program which would be used in this program as well.


Here's the link to that article:

WAP (Python) to print the nth term of the Fibonacci Series


I hope that you are now clear with the basics of the Fibonacci Series. Now, let's start with the Overview!


Overview:


To write the code for this program, we will use the basic knowledge that each term of the Fibonacci Series is the sum of the previous two terms. This statement is the heart of our program.


This means that in the program, we have to make three variables. The first and second variable would have the value 0 and 1 respectively (as the series starts from 0 and 1) and another variable that would carry their sum. This way we print the first term.


To print the next term, we need the first variable to become equal to the second one and the second variable to become equal to the third variable. And then the above process will run again to print the second term and so on. 


For example: Let first variable be 0, and the second be 1. Then the next term would be 0+1 = 1. After this, the first variable will become 1 and the second will become 1. Hence, the next term would become 1+1 = 2. And this would continue till n terms.


I hope that now you have understood the basics of this program. For this program, we will use the while loop, that will execute the above process till it prints the required series.


*As I have already told you the basics of this program, you could try to do it on your own. See if the solution matches.*


Understanding what we need to do:


Here I will tell you step-by-step what you need to do.


Step 1: Asking the user for the input


This is the easiest part of the program. The only input we need from the user is the number of terms in the series. We have assigned it the variable 'n'.  Also, don't forget to typecast it into an integer using the int function as we are going to do calculations on it. 


Here's the code for this step:


Step 2: Setting the stage for the actual program


Now, as we know that the Fibonacci series starts with 0 and 1. Hence, we want them to be stored in the first two variable ('a' and 'b' namely).


We will also make a variable 'count' that will keep track on the number of terms already printed. We will use this variable as the condition for the while loop afterwards. For now, its value is initialized to be 0.


Here' s the code for this step:


* As we can't concatenate an integer with a string directly, hence we have used the literals 'f' and '{n}' to do it in an easier way. * 


Step 3: Printing the n terms of the Fibonacci Series


Now PAY ATTENTION! This step is the soul of our program.


As we have already discussed in the Overview, the algorithm associated with this program is that 'to print each term, we need to add the previous two terms'. Hence we are going to follow the same rule here.


Now, we need another variable 'c' that would contain the sum of the previous two variable 'a' and 'b'. Firstly we would print the variable 'a' i.e., 0. Then we would add 'a' and 'b' (i.e., 0 +1) to get 'c'.


After this, we would declare the value of 'a' to be equal to 'b' (i.e., 1) and the value of 'b' to be equal to 'c' (i.e., 0+1 =1). This was the main process that would take place.


This all is contained under a while loop which would run till count<n. After each process, the value of count (initially 0) will be iterated by 1


After completing one process, the while loop runs again. Now the value of 'a' (i.e., 1) is printed and the rest of steps are continued again. Hence, its the variable 'a' that contains and prints all the terms of the Fibonacci series, one-by-one. 


I hope you were able to understand this step. Please read the chronology of the program (written below) to understand the program in a better way.


Here's the code for this step:


So now, we have found the best way to print the Fibonacci Series in Python!


Here's the chronology of the program:

  1. User enters the number of terms of the Fibonacci Series to be printed.
  2. Two variables, 'a' and 'b' containing the first two terms of the series (i.e., 0 and 1) are made.
  3. Another variable count is made that would help in controlling the while loop.
  4. While loop runs with the condition count<n.
  5. Inside the while loop, first the variable 'a' is printed.
  6. Then another variable 'c' is declared to be equal to the sum of 'a' and 'b'. 
  7. The value of 'a' is assigned to be equal to 'b'. And, the value of 'b' is assigned to be equal to 'c'.
  8. The value of 'count' is increased by 1.
  9. And the same loop runs again and again till count<n.
  10. It's the variable 'a', which we are printing in the 5th step, that is making and printing our Fibonacci Series.

I hope that now the whole program is understood by you. If still the program is not clear to you, then I would suggest you to play with the program yourself and test the significance of each line of code. But then also, the comments section is always there to help you out!

Here's the final code:

Here's the output of our program:

Program to print the Fibonacci Series in Python

This is the end of our amazing journey where we found the best way to print the Fibonacci Series in Python! 

I hope you liked the tutorial. If you did, then don't forget to share this tutorial to others as this may help someone struggling in coding.

Also, don't forget to subscribe to our newsletter as we do these coding sessions regularly!

Again, thanks for reading!

No comments:

Post a Comment