Python Exercise: Fibonacci series between 0 to 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:
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:
Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
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".
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics