We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 48a392b commit d6a4159Copy full SHA for d6a4159
fibonacci_series.py
@@ -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
23
+ nth = n1 + n2
24
+ # update values
25
+ n1 = n2
26
+ n2 = nth
27
+ count += 1
0 commit comments