Fibonacci series are a sequence of integer numbers. It starts from 0 and 1. The sequence is obtained by adding the preceding two terms. In mathematical terms it is represented by Fn = Fn-1 + Fn-2. Here, Fn is the nth term, Fn-1 and Fn-2 are the two preceding terms. The following program shows a Fibonacci sequence up to nth term.
# Program to write the Fibonacci series up to the nth term
nthterm = int(input("Write the number of nth term up to which you want to find the sequence"))
# For a fibonacci series first and second terms is 0 and 1
firstterm = 0
secondterm = 1
addup = 0
# If the user enters an invalid input
if nthterm <= 0:
print("You need to enter a number greater than 0 and 1")
elif nthterm == 1:
print("Second term of the fibonacci series is ", nthterm, ":")
print(firstterm, secondterm)
else:
print("The Fibonacci series for the entered nth term is:")
while addup < nthterm:
print(firstterm)
lastterm = firstterm + secondterm
firstterm = secondterm
secondterm = lastterm
addup += 1