Q:

Display Fibonacci series up to 10 terms using python programming

belongs to collection: Python Loop Exercises

0

Display Fibonacci series up to 10 terms

The Fibonacci Sequence is a series of numbers. The next number is found by adding up the two numbers before it. The first two numbers are 0 and 1.

For example, 0, 1, 1, 2, 3, 5, 8, 13, 21. The next number in this series above is 13+21 = 34.

Expected output:

Fibonacci sequence:
0  1  1  2  3  5  8  13  21  34

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Set num1 = 0 and num2 =1 (first two numbers of the sequence)
  • Run loop ten times
  • In each iteration
    • print num1 as the current number of the sequence
    • Add last two numbers to get the next number res = num1+ num2
    • update values of num1 and num2. Set num1=num2 and num2=res

Solution:

# first two numbers
num1, num2 = 0, 1

print("Fibonacci sequence:")
# run loop 10 times
for i in range(10):
    # print next number of a series
    print(num1, end="  ")
    # add last two numbers to get next number
    res = num1 + num2

    # update values
    num1 = num2
    num2 = res

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to use the loop to find the... >>
<< Write a python program to display all prime number...