Answer to Question #208953 in Electrical Engineering for mona

Question #208953

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


1
Expert's answer
2021-06-21T05:52:29-0400
# 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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS