- Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathfibonacci.py
More file actions
Latest commit
23 lines (20 loc) · 496 Bytes
/
Copy pathfibonacci.py
File metadata and controls
23 lines (20 loc) · 496 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Program to display the Fibonacci sequence up to n-th term
nterms=int(input("How many terms? "))
# first two terms
n1, n2=0, 1
count=0
# check if the number of terms is valid
ifnterms<=0:
print("Please enter a positive integer")
elifnterms==1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
else:
print("Fibonacci sequence:")
whilecount<nterms:
print(n1)
nth=n1+n2
# update values
n1=n2
n2=nth
count+=1