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

BoiCoder

Coding Made Easy for All

Breaking

Wednesday, 23 September 2020

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


Hey all, today we are going to find the nth term of the Fibonacci Series. So gear up as we are going to solve this problem in the best way possible. We are going to follow a step-wise approach while solving this question.


If you want to see the final code of this problem, it is available at the end of the article. But it would be better if you stay with us as we are going to go through each step you should follow to become a successful programmer. Excited? So open up your code editor and let's start!!!

What is a Fibonacci Series?


In simple words, Fibonacci series is a sequence of numbers following the format of 0, 1, 1, 2, 3, 5, 8..... and so on. You can clearly see that their is a specific pattern in this arrangement. Starting from the second term, we can see that the third term is the addition of the first and the second term and the fourth term is the addition of the third and second term and this continues forever. 

* Note that, I have used the words first term and second term just to make you understand. But while finding the nth term, we often ignore 0 and hence our counting starts from 1. Hence the 5th term of the Fibonacci Series is not 3 but 5!*

Overview: 


In this program, we are asked to find the nth term of the Fibonacci Series. The input required would be the value of 'n' and then we would be using for loop to find the term that corresponds to the given value of 'n'.

*As I have already told you the main steps of this program, you can try to do it on your own. See if your solution matches ours!!!*

Understanding what we need to do:


Here, I would tell you step by step what you need to do.

Step 1: Asking the user for the input:


The first step would be to ask the user for the input. Let us name the variable as 'n'. While asking for the input, we will also make sure that we type-convert the value to integer. This is because the default data type of input is string, but to do calculations on it, we need to convert its type to integer using the 'int' command.

Here's the code for the first step:

Step 2: Finding the nth term of the Fibonacci Series


*Note that, there are mainly two ways to solve this question, one is 'Iterative method', which we are using in this article, and the other one is the 'Recursive method', which is somewhat advanced and there's actually no benefit of using it. If you want it too, then please tell us in the comments section.*

Now that we have got the value of 'n', let's start with the real program. As you can see, to make this program work, we need three variables, one that holds current value, one that holds previous value and one that holds previous to previous value. 

In this program, we have taken the previous value to be 0, the current value to be 1 and the previous to previous value has been initialized inside the loop to be equal to previous number, because that's what we want. 

Then we write the for loop in range of (1, n)  *Note that for loops run from the start point to the (end point-1). Hence we are actually executing the loop from 1 to n-1*

Inside the loop body, we first declare the prevprevnum to be equal to the prevnum and then the prevnum to be equal to the currentnum. Then I will initialize the value of the currentnum  to be (prevnum+prevprevnum). Then the loop will iterate till it reaches n. I hope you are able to understand this part.

If you still didn't understand, then first see the code, then ask yourself about the significance of each step. Also, the best way to do these type of programs is to think how your mind would have solved this. If you do this one step, then you have taken one more step to the path of becoming the best coder!

Here's the code of the Step 2:

So now, we have found the best way to print the nth term of the Fibonacci Series. 

Here's the full code:

And here ends our amazing journey where we found the best possible way to print the nth term of the Fibonacci Series in Python. I hope that you liked this article, if you did then don't forget to share it, so that everyone in need can get help. 

Here's your next challenge:

WAP to find if a number is an Armstrong Number or not.

In the next article, we would be finding the answer to the above question, till then keep coding!

Also please consider subscribing to our newsletter as we do these coding sessions regularly. 

Again, thanks for reading!!!

No comments:

Post a Comment