w3resource

Python Exercise: Fibonacci series between 0 to 50


9. Fibonacci Series Between 0 and 50

Write a Python program to get the Fibonacci series between 0 and 50.

Note : The Fibonacci Sequence is the series of numbers :
0, 1, 1, 2, 3, 5, 8, 13, 21, ....
Every next number is found by adding up the two numbers before it.

Pictorial Presentation:

Python Exercise: Fibonacci series between 0 to 50

Sample Solution:

Python Code:

# Initialize variables 'x' and 'y' with values 0 and 1, respectively
x, y = 0, 1

# Execute the while loop until the value of 'y' becomes greater than or equal to 50
while y < 50:
    # Print the current value of 'y'
    print(y)
    
    # Update the values of 'x' and 'y' using simultaneous assignment,
    # where 'x' becomes the previous value of 'y' and 'y' becomes the sum of 'x' and the previous value of 'y'
    x, y = y, x + y

Sample Output:

1                                                                                                             
1                                                                                                             
2                                                                                                             
3                                                                                                             
5                                                                                                             
8                                                                                                             
13                                                                                                            
21                                                                                                            
34 

Flowchart:

Flowchart: Python program to get the Fibonacci series between 0 to 50

For more Practice: Solve these Related Problems:

  • Write a Python program to generate the Fibonacci sequence up to 50 using a while loop.
  • Write a Python program to use recursion to print all Fibonacci numbers less than 50.
  • Write a Python program to build the Fibonacci series up to a given limit and store the result in a list.
  • Write a Python program to implement the Fibonacci sequence using list comprehension and a generator function.

Go to:


Previous: Write a Python program that prints all the numbers from 0 to 6 except 3 and 6.
Next: Write a Python program which iterates the integers from 1 to 50. For multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.