Skip to content

Commit cb44cb1

Browse files
authored
Create Fibonacci.py
1 parent 819f36f commit cb44cb1

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Fibonacci.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
nterms = int(input("How many terms? "))
2+
3+
# first two terms
4+
n1, n2 = 0, 1
5+
count = 0
6+
7+
# check if the number of terms is valid
8+
if nterms <= 0:
9+
print("Please enter a positive integer")
10+
# if there is only one term, return n1
11+
elif nterms == 1:
12+
print("Fibonacci sequence upto",nterms,":")
13+
print(n1)
14+
# generate fibonacci sequence
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)