Program for Fibonacci Series in Python

Program for Fibonacci Series in Python

In this blog we will get to know about The Fibonacci Series and also about its process of finding with the help of programming language. In Coding we use the famous and the simplest syntax Programming Language which is Python.

What is Fibonacci Series?

The Fibonacci series is a mathematical sequence of numbers in which, each element is the sum of the previous two elements. It starts with zero and one followed by 1, 2, 3, 5, 8, 13, 21, and so on. Each element in this sequence is called a Fibonacci number. In this article, we will see a Python program to print the Fibonacci series in Python. also Fibonacci Series is representation of the beautiful things in nature. such as the Golden Ratio.

The whole Fibonacci Series is comes from the an Italian Mathematician named Fibonacci also known as Leonardo Bonacci, Leonardo of Pisa, or Leonardo Bigollo Pisano.<Click here for more info>


Program for Fibonacci Series in Python

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8....

0 and 1 are the two term. All other terms are executed by adding the preceding or last two terms . This means to say the nth term is the sum of (n-1)th and (n-2)th term.

1) Program for Fibonacci Series in Python using If. Else Statement and While loop Without Function

Source Code

Output


Here, we store the number of terms in nth terms. We initialize the first term to 0 and the second term to 1.

If the number of terms is more than 2, we use a while loop to find the next term in the sequence by adding the preceding two terms. We then interchange the variables (update it) and continue on with the process.

2) Program for Fibonacci Series in Python using While Loop without Function

Source Code

n = 10
num1 = 0
num2 = 1
next_number = num2 
count = 1
 
while count <= n:
    print(next_number, end=" ")
    count += 1
    num1, num2 = num2, next_number
    next_number = num1 + num2
print()

Output

1 2 3 5 8 13 21 3455 89 

3) Program for Fibonacci Series in Python using If. Else Statement With Function

Source Code

def Fibonacci(n):
 
    # Check if input is 0 then it will
    # print incorrect input
    if n < 0:
        print("Incorrect input")
 
    # Check if n is 0
    # then it will return 0
    elif n == 0:
        return 0
 
    # Check if n is 1,2
    # it will return 1
    elif n == 1 or n == 2:
        return 1
 
    else:
        return Fibonacci(n-1) + Fibonacci(n-2)
 
 
# Driver Program
print(Fibonacci(9))

Output

34
Hope you guys Understood this concept and do implement it to your coding platform. and there are many method to program Fibonacci Series in python programming. 

Post a Comment

Previous Post Next Post