Skip to content

Commit 8d2b5d7

Browse files
authored
fibonacci
Python Program to Print the Fibonacci sequence
1 parent f581e7e commit 8d2b5d7

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

fibonacci.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Program to display the Fibonacci sequence up to n-th term
2+
3+
nterms = int(input("How many terms? "))
4+
5+
# first two terms
6+
n1, n2 = 0, 1
7+
count = 0
8+
9+
# check if the number of terms is valid
10+
if nterms <= 0:
11+
print("Please enter a positive integer")
12+
elif nterms == 1:
13+
print("Fibonacci sequence upto",nterms,":")
14+
print(n1)
15+
else:
16+
print("Fibonacci sequence:")
17+
while count < nterms:
18+
print(n1)
19+
nth = n1 + n2
20+
# update values
21+
n1 = n2
22+
n2 = nth
23+
count += 1

0 commit comments

Comments
 (0)