Fibonacci sequence
Aim: Implement
a program to find the Fibonacci sequence up to n-th term where n is provided by
the user.
Algorithm:
1:
Start
2:
set a=0,b=1,c=0,i=1
3:Input
value for n
4:print
a,b
5:
if i<=n go to step6 else go to step 10
6:c=a+b
7:print
c
8:a=b,b=c,i=i+1
9:repeat
step 5
10:stop
Program:
#
Program to display
#
take input from the user
nterms
= int(input("How many terms? "))
#
first two terms
a
= 0
b
= 1
c=0
print
a,b
for
i in range (2,n-1):
c=a+b
a=b
b=c
print c
Output:
How
many terms? 10
Fibonacci
sequence:
0,
1, 1, 2, 3, 5, 8, 13, 21, 34,
0 Comments:
Post a Comment