Skip to content

Latest commit

 

History

History
86 lines (68 loc) · 3.15 KB

File metadata and controls

86 lines (68 loc) · 3.15 KB

Write a program to print fibonacci series.

Here’s a Python program to print the Fibonacci series up to a specified number of terms:

# Function to print Fibonacci series
def fibonacci_series(n):
    # First two terms
    a, b = 0, 1
    print("Fibonacci Series:")
    
    for i in range(n):
        print(a, end=" ")
        # Update the values of a and b
        a, b = b, a + b

# Input number of terms from the user
n = int(input("Enter the number of terms: "))

# Call the function
fibonacci_series(n)

This program prompts the user to input the number of terms and then prints the Fibonacci series accordingly.

The provided Fibonacci series program works as expected, but there are a few potential issues and areas for improvement:

1. No Input Validation:

  • Problem: If the user inputs a non-integer value or a negative number, the program will either crash or produce incorrect results.
  • Improvement: Add input validation to ensure the input is a positive integer.

2. No Handling for Edge Cases:

  • Problem: If the user enters 0 or 1 as the number of terms, the program will behave strangely by printing nothing or just 0.
  • Improvement: Add specific conditions to handle the cases where the input is 0 or 1.

3. Print Inside the Function:

  • Problem: Printing the series inside the loop limits flexibility (e.g., reusing the function to return the series instead of just printing it).
  • Improvement: Modify the function to return the Fibonacci series as a list, allowing it to be reused elsewhere or printed later.

4. Use of an Iterative Approach:

  • Improvement Option: While the iterative approach is efficient, a recursive approach can provide a more elegant solution, though at the cost of performance for large inputs.

Improved Version with Error Handling and Flexibility:

def fibonacci_series(n):
    # Check for invalid input
    if n < 0:
        return "Invalid input! Please enter a positive integer."
    elif n == 0:
        return []
    elif n == 1:
        return [0]
    
    # Generate the Fibonacci series
    fib_series = [0, 1]
    for i in range(2, n):
        fib_series.append(fib_series[-1] + fib_series[-2])
    
    return fib_series

# Input number of terms with validation
while True:
    try:
        n = int(input("Enter the number of terms: "))
        if n < 0:
            print("Please enter a positive integer.")
        else:
            break
    except ValueError:
        print("Invalid input! Please enter an integer.")

# Call the function and print the series
result = fibonacci_series(n)
if isinstance(result, list):
    print("Fibonacci Series:", " ".join(map(str, result)))
else:
    print(result)

Key Improvements:

  1. Input Validation: The program now checks whether the input is a valid integer and ensures that it is non-negative.
  2. Handling Edge Cases: It handles edge cases for 0 and 1 terms.
  3. Return Value: The function now returns a list of Fibonacci numbers, allowing for flexibility (e.g., printing, further manipulation, etc.).
  4. User-Friendly Messages: Added appropriate messages for invalid input.