Skip to content

Commit d6a4159

Browse files
authored
Create fibonacci_series.py
1 parent 48a392b commit d6a4159

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

fibonacci_series.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
13+
# if there is only one term, return n1
14+
elif nterms == 1:
15+
print("Fibonacci sequence upto",nterms,":")
16+
print(n1)
17+
18+
# generate fibonacci sequence
19+
else:
20+
print("Fibonacci sequence:")
21+
while count < nterms:
22+
print(n1)
23+
nth = n1 + n2
24+
# update values
25+
n1 = n2
26+
n2 = nth
27+
count += 1

0 commit comments

Comments
 (0)