Display Terms of Fibonacci Series

AIM

To write a Python program that displays the terms of a Fibonacci series up to n terms as specified by the user.

Fibonacci Series:

A sequence where each number is the sum of the two preceding ones. The series starts with 0 and 1.
Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

ALGORITHM

  1. Start
  2. Initialize first two terms: n1 = 0, n2 = 1
  3. Input the number of terms from the user
  4. Check if terms is less than or equal to 0 - display error message
  5. If terms = 1, print only the first term (0)
  6. If terms ≥ 2, print first two terms (0, 1)
  7. Use a loop from 2 to terms:
    • Calculate next_term = n1 + n2
    • Print next_term
    • Update n1 = n2 and n2 = next_term
  8. Stop

PROGRAM

# Display the terms of a Fibonacci series

n1 = 0
n2 = 1
terms = int(input("Enter the Terms of Fibonacci series you want to print: "))

if terms <= 0:
    print("Enter positive value of term.")
else:
    if terms == 1:
        print(n1, end=" ")
    else:
        print(n1, n2, end=" ")
        for i in range(2, terms):
            next_term = n1 + n2
            print(next_term, end=" ")
            n1 = n2
            n2 = next_term

OUTPUT

Test Case 1 (10 terms):

Enter the Terms of Fibonacci series you want to print: 10

0 1 1 2 3 5 8 13 21 34

Test Case 2 (5 terms):

Enter the Terms of Fibonacci series you want to print: 5

0 1 1 2 3

Test Case 3 (1 term):

Enter the Terms of Fibonacci series you want to print: 1

0

Test Case 4 (Invalid input):

Enter the Terms of Fibonacci series you want to print: -2

Enter positive value of term.

CONCLUSION

Thus, the given program was successfully executed and the output was verified as per the expected result.

VIVA QUESTIONS

  1. What is the Fibonacci series and where is it found in nature?

    The Fibonacci series is a sequence where each number is the sum of two preceding ones (0, 1, 1, 2, 3, 5, 8...). It appears in nature in flower petals, pinecones, shells, and spiral galaxies.

  2. Why do we initialize n1=0 and n2=1?

    These are the first two terms of the Fibonacci series by definition. The series starts with 0 and 1, and each subsequent term is calculated from these initial values.

  3. What is the purpose of the end=" " parameter in print()?

    The end=" " parameter prevents the print function from adding a newline character, allowing all numbers to be printed on the same line separated by spaces.

  4. How does the variable swapping work in this program?

    After calculating next_term, we update n1=n2 and n2=next_term. This shifts the values forward so the next iteration can calculate the subsequent term correctly.

  5. Can you write a recursive version of this program?

    Yes, using recursion: def fib(n): return n if n <= 1 else fib(n-1) + fib(n-2). However, the iterative approach is more efficient for large numbers.

  6. What happens if the user enters 0 or negative number?

    The program displays "Enter positive value of term." because the Fibonacci series requires at least one term to be meaningful.

  7. What is the mathematical relationship in Fibonacci numbers?

    Each term equals the sum of the two preceding terms: F(n) = F(n-1) + F(n-2), where F(0)=0 and F(1)=1.

  8. How would you find the nth Fibonacci number without printing the series?

    Use the same logic but don't print intermediate values. Keep updating n1 and n2 until you reach the nth position, then return n2.