Example output
Here's a Python code snippet that implements a recursive function to calculate the Fibonacci sequence up to the nth term:
def fibonacci(n):
# Base cases: Fibonacci of 0 is 0, Fibonacci of 1 is 1
if n == 0:
return 0
elif n == 1:
return 1
else:
# Recursive case: Fibonacci of n is the sum of Fibonacci of (n-1) and Fibonacci of (n-2)
return fibonacci(n - 1) + fibonacci(n - 2)
# Test the function with various values of n
test_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for n in test_values:
print(f"Fibonacci({n}): {fibonacci(n)}")
This code defines a function fibonacci(n) that recursively calculates the nth Fibonacci number. It uses base cases for n=0 and n=1, and for other values of n, it recursively calculates the Fibonacci number by adding the previous two Fibonacci numbers. Finally, it tests the function with various values of n to ensure correctness and robustness.