Hi Guys, welcome to my blog in this article you will learn about how to Write A Python Program To Print Fibonacci Series Up To 50
Title :
Write A Python Program To Print Fibonacci Series Up To 50
Description :
Hi guys in this article you will learn about a python program that will print the fibonacci numbers upto 50
Fibonacci numbers of a number is defined by adding the number with its preceding number
fib(n) = fib(n) + fib(n-1)
This python program does not require any input from the user just define value of a, b and limit as 0, 1 and 50
a, b, limit = 0, 1, 50
print(a, end=", ")
print(b, end=", ")
Now in order to find the fibonacci numbers until we reach 50 we will use infinite while loop and keep adding a, b variables until it reaches 50 Also check if addition of two variables a and b not reached until 50, keep printing c value and swap a and b values at the end.
while True:
c = a + b
if c > limit:
break
print(c, end=", ")
a, b = b, c
Complete Python Code :
a, b, limit = 0, 1, 50
print(a, end=", ")
print(b, end=", ")
while True:
c = a + b
if c > limit:
break
print(c, end=", ")
a, b = b, c
Output :
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Output :
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Conclusion :
The above Python program takes two variables a and b and uses infinite while loop to calculate the fibonacci series upto 50.
In the while loop the program keeps adding a dn b values, storing the sum in the c variable. Variable is checked if it's less than 50 and it gets printed else the program exits using break/ exit(0) function.
Comment it down below if you have any query is regarding the above Python program
0 Comments