Python Program for Fibonacci Series Using Recursion

Python Program for Fibonacci Series Using Recursion
Reading Time: 4 minutes

Introduction:

The given Python program implements the Fibonacci series using recursion. The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones. It starts with 0 and 1, and each subsequent number is the sum of the two preceding numbers. The program also includes a function to print the Fibonacci series up to a given number of terms.

Algorithm:

  1. Fibonacci(n) function:
    • Check if the input n is less than or equal to 0. If true, return the string “Invalid input. The input should be a positive integer.” This step handles invalid inputs.
    • Check if n is equal to 1. If true, return 0. This is the first number in the Fibonacci series.
    • Check if n is equal to 2. If true, return 1. This is the second number in the Fibonacci series.
    • If the above conditions are not met, recursively call the Fibonacci ( ) function with n – 1 and n – 2, and return the sum of the two recursive calls. This step calculates the Fibonacci number for the given input n.
  2. Print _ Fibonacci _series(n) function:
    • Check if the input n is less than or equal to 0. If true, print “Invalid input. The input should be a positive integer.” to handle invalid inputs.
    • If n is greater than 0, print “Fibonacci Series:” to indicate the output will be the Fibonacci series.
    • Iterate from 1 to n (inclusive) using a for loop:
      • Call the Fibonacci ( ) function with the current loop index as the argument to calculate the Fibonacci number for each term.
      • Print the calculated Fibonacci number using print ( ) with the end parameter set to ” ” to display the numbers on the same line.

Explanation:

The program consists of two functions, Fibonacci(n) and print _ Fibonacci _series(n). The Fibonacci (n) function calculates the nth Fibonacci number using recursion, and the print _ Fibonacci _series (n) function prints the Fibonacci series up to the nth term.

When the program is executed, it first defines the Fibonacci () and print _ Fibonacci _series ( ) functions. Then, it tests the program by calling print _ Fibonacci _series() with n _terms = 10. This will generate and print the first 10 terms of the Fibonacci series.

The Fibonacci series for n_terms = 10 is as follows:

Python Code:

				
					def fibonacci(n):
    if n <= 0:
        return "Invalid input. The input should be a positive integer."
    elif n == 1:
        return 0
    elif n == 2:
        return 1
    else:
        return fibonacci(n - 1) + fibonacci(n - 2)
def print_fibonacci_series(n):
    if n <= 0:
        print("Invalid input. The input should be a positive integer.")
    else:
        print("Fibonacci Series:")
        for i in range(1, n + 1):
            print(fibonacci(i), end=" ")
# Test the program
n_terms = 10  # Change this value to generate more or fewer terms in the series
print_fibonacci_series(n_terms)

				
			

Output:

				
					Fibonacci series : 0 1 1 2 3 5 8 13 21 34
				
			

You can change the value of n_terms to generate a longer or shorter Fibonacci series. Keep in mind that using recursion for larger values of n_terms may lead to slower execution and consume more memory, as recursion has exponential time complexity. For more efficient Fibonacci generation, consider using an iterative approach or memorization techniques.

Ethical Hacking Course Quick Enquiry Form

Fill the Form Get Free Consultation From Industry Experts!

Quick Enquiry Form

Fill the Form Get Free Consultation From Industry Experts!