Python program to find Fibonacci Series upto nth number

Write a python program to print the series up to the nth term of the Fibonacci Series. In this Python Program let’s find the Fibonacci series up to the nth number.

What is Fibonacci Series?

Fibonacci series is the successive sum of two preceding terms to give the next one the sequence is always  like 0,1,1,2,3,5,8,13,21,34,55…. and so on

here if we took any digit let say 55 its’s summed up by preceding two digits that are 21 and 34

Algorithm to find Fibonacci Series

  • Take input and initialize variables
  • Performing the operation on it logically
  • Printing appropriate message at the end

Python program to find Fibonacci Series upto nth number

n=int(input("enter the number of terms "))
a=0
b=1
if n<=0:
    print("enter a valid number ")
elif n==1:
    print(a)
else:
    print(a)
    print(b)
    for i in range(2,n):
        c=a+b
        a=b
        b=c
        print(c)

Explanation of the code

On-Line no:1 taking the input and converting it into integer type using function

On-Line no:2 declaring the variable and initialize with zero

On-Line no:3 declaring the variable and initialize with one

OnLine no:4 checking for the taken input, the taken input should be greater than zero  here we check for the input whether it satisfies the condition or not

On-Line no:5 if the number is zero or less than zero it will print a message to the user asking to input the proper and valid number

On-Line no:6  another condition to be checked here if the inputted value is one then we will print zero as the zero is the first element of the Fibonacci series

On-Line no:7 printing the zero

On-Line no:8 now comes to the else block

On-Line no:9 and On-Line no:10 printing the starting number that is zero and one

On-Line no:11  using for loop to iterate the block of code again and again and starts from 2 and end on nth term given by the user

On-Line no:12 assigning the value to a variable that holds the sum of two preceding terms

On-Line no:13 and On-Line no:14 swap the values for further use

On-Line no:15 printing the result we get from the logic we used

Output

In this blog, we learned how to create a python program to print the Fibonacci series up to nth term