Write a Python Program to Print the Fibonacci sequence.
Fibonacci sequence:
The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. So, the sequence begins with 0 and 1, and the next number is obtained by adding the previous two numbers. This pattern continues indefinitely, generating a sequence that looks like this:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on.
Mathematically, the Fibonacci sequence can be defined using the following recurrence relation:
𝐹(0) = 0𝐹(1) = 1𝐹(𝑛) = 𝐹(𝑛−1)+𝐹(𝑛−2)𝑓𝑜𝑟𝑛 > 1
Program
def myfun():
nterms=int(input("How many terms? "))
n1=0
n2=1
count=0
if(nterms<=0):
print("Please enter a positive integer")
elif(nterms==1):
print("Fibonacci sequence upto",nterms," : ")
print(n1)
else:
print("Fibonacci sequence:")
while(count<nterms):
print(n1)
nth=n1+n2
# update values
n1=n2
n2=nth
count=count+1
if __name__=="__main__":
myfun()
Output:
How many terms? 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
More Questions
69 . Write a Python Program to Display the multiplication Table.
70 . Write a Python Program to Print the Fibonacci sequence.
71 . Write a Python Program to Check Armstrong Number?
72 . Write a Python Program to Find Armstrong Number in an Interval.
73 . Write a Python Program to Find the Sum of Natural Numbers.
74 . Write a Python Program to Find LCM.
75 . Write a Python Program to Find HCF.
76 . Write a Python Program to Convert Decimal to Binary, Octal and Hexadecimal.
77 . Write a Python Program To Find ASCII value of a character.
78 . Write a Python Program to Make a Simple Calculator with 4 basic mathematical operations.
79 . Write a Python Program to Display Fibonacci Sequence Using Recursion.
For latest job updates join Telegram Channel: https://t.me/sateeshm