# File: rfib.py # Program to compute the first n numbers of the Fibonacci series recursively # # Matt Bishop, MHI 289I, Fall 2024 # # get the nth Fibonacci number # def fib(n): # base cases: f0 = 0, f1 = 1 if n == 0: return 0 elif n == 1: return 1 # recursion: fn = fn-1 + fn-2 return fib(n-1) + fib(n-2) # # main routine # # input number try: n = int(input("Fibonacci sequence from f0 to f: ")) except: print("Need an integer") else: # it better be non-negative! if n < 0: print("Need a non-negative integer") else: # compute the sequence and print the terms for i in range(n+1): # get next Fibonacci number and # announce it print("Fibonacci number", i, "is", fib(i))