Write and execute a PYTHON program to perform the following tasks.
2.To print the following series using while loop. 1, 1, 2 ,3 ,5, 13,18 ...n
# Program to display the Fibonacci sequence up to nth term
nth = int(input("Enter number of terms: "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is positive
if nth <= 0:
print("Please enter a positive integer:")
# if nth is one term
elif nth == 1:
print("Fibonacci sequence upto",nth,":")
print(n1)
# generate fibonacci sequence of nth terms
else:
print("Fibonacci sequence:")
while count < nth:
print(n1)
newth = n1 + n2
# update values of n1 and n2
n1 = n2
n2 = newth
count += 1
Comments
Leave a comment