Python Program To Print Fibonacci Series

Python Program To Print Fibonacci Series

01. Method

Fibonacci series without using recursion.

def fibonacci(n):
    n1 = 0
    n2 = 1
    sum = 0
    if n == 1:
        print("Fibo Series: ",n1,end="")
    elif(n == 2):
        print("Fibo Series: ",n1, " ", n2,end="")
    else:
        print("Fibo Series: ",n1, " ", n2,end="")
        for i in range(2, n):
            sum = n1 + n2
            n1 = n2
            n2 = sum
            print(" ",sum,end="")
            

# N = int(input("Enter the input: ", ))
N = 8
fibonacci(N)

Output:

Fibo Series:  0   1  1  2  3  5  8  13

Read Also: 10+ Python Pattern Programs Using For Loop

02. Method

Fibonacci series using recursion.

def fibonacci(n):
    if n == 0:
        return 0    
    if n == 1:
        return 1
    else:
        return (fibonacci(n-2) + fibonacci(n-1))

# N = int(input("Enter the input: ", ))
N = 8
print("Fibo Series: ",end="")
for i in range(0,N):
    print(fibonacci(i),end=" ")

Output:

Fibo Series:  0   1  1  2  3  5  8  13

 

USEFUL POSTS:

  1. Python program to append entire list to another list
  2. Python program to calculate Mode using List
  3. Python program to calculate Median Using List

Leave a comment