File tree 3 files changed +84
-0
lines changed
Dynamic Programming/Fibonacci Sequence
3 files changed +84
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Program to display the Fibonacci sequence up to n-th term where n is provided by the user
2
+
3
+ # change this value for a different result
4
+ nterms = int (input("enter the no. of terms " ))
5
+
6
+ # uncomment to take input from the user
7
+ #nterms = int(input("How many terms? "))
8
+
9
+ # first two terms
10
+ n1 = 0
11
+ n2 = 1
12
+ count = 0
13
+
14
+ # check if the number of terms is valid
15
+ if nterms <= 0:
16
+ print("Please enter a positive integer")
17
+ elif nterms == 1:
18
+ print("Fibonacci sequence upto",nterms,":")
19
+ print(n1)
20
+ else:
21
+ print("Fibonacci sequence upto",nterms,":")
22
+ while count < nterms:
23
+ print(n1,end=' , ')
24
+ nth = n1 + n2
25
+ # update values
26
+ n1 = n2
27
+ n2 = nth
28
+ count += 1
Original file line number Diff line number Diff line change
1
+ # Program to display the Fibonacci sequence up to n-th term where n is provided by the user
2
+
3
+ # change this value for a different result
4
+ nterms = int (input ("enter the no. of terms " ))
5
+
6
+ # uncomment to take input from the user
7
+ #nterms = int(input("How many terms? "))
8
+
9
+ # first two terms
10
+ n1 = 0
11
+ n2 = 1
12
+ count = 0
13
+
14
+ # check if the number of terms is valid
15
+ if nterms <= 0 :
16
+ print ("Please enter a positive integer" )
17
+ elif nterms == 1 :
18
+ print ("Fibonacci sequence upto" ,nterms ,":" )
19
+ print (n1 )
20
+ else :
21
+ print ("Fibonacci sequence upto" ,nterms ,":" )
22
+ while count < nterms :
23
+ print (n1 ,end = ' , ' )
24
+ nth = n1 + n2
25
+ # update values
26
+ n1 = n2
27
+ n2 = nth
28
+ count += 1
Original file line number Diff line number Diff line change
1
+ # Program to display the Fibonacci sequence up to n-th term where n is provided by the user
2
+
3
+ # change this value for a different result
4
+ nterms = int (input("enter the no. of terms " ))
5
+
6
+ # uncomment to take input from the user
7
+ #nterms = int(input("How many terms? "))
8
+
9
+ # first two terms
10
+ n1 = 0
11
+ n2 = 1
12
+ count = 0
13
+
14
+ # check if the number of terms is valid
15
+ if nterms <= 0:
16
+ print("Please enter a positive integer")
17
+ elif nterms == 1:
18
+ print("Fibonacci sequence upto",nterms,":")
19
+ print(n1)
20
+ else:
21
+ print("Fibonacci sequence upto",nterms,":")
22
+ while count < nterms:
23
+ print(n1,end=' , ')
24
+ nth = n1 + n2
25
+ # update values
26
+ n1 = n2
27
+ n2 = nth
28
+ count += 1
You can’t perform that action at this time.
0 commit comments