Write a Program in Python to Print Fibonacci sequences.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Limit = int(input ("How many terms the user wants to print? ")) # First two terms T4Tutorials_1 = 0 T4Tutorials_2 = 1 count = 0 # Now, we will check if the number of terms is valid or not if Limit <= 0: print ("Please enter a positive integer, the given number is not valid") # if there is only one term, it will return T4Tutorials_1 elif Limit == 1: print ("The Fibonacci series of the numbers up to", Limit, ": ") print(T4Tutorials_1) # Then we will generate Fibonacci series of number else: print ("Congratulations! The fibonacci series of the numbers is:") while count < Limit: print(T4Tutorials_1) nth = T4Tutorials_1 + T4Tutorials_2 # At last, we will update values T4Tutorials_1 = T4Tutorials_2 T4Tutorials_2 = nth count += 1 |
Output
How many terms the user wants to print? 9
Congratulations! The Fibonacci series of the numbers is:
0
1
1
2
3
5
8
13
21