In this program, we will see how to Print Fibonacci Sequence.
Fibonacci Sequence is the sum of the two numbers that precede it.
# 0 1 1 2 3 5 8 13 21........
In this program, we are using if, elif, else conditions & a for loop.
Follow the code given below:
def fib(n):
a=0
b=1
if n<=0:
print('Incorrect input!')
elif n==1:
print(a)
else:
print(a)
print(b)
for i in range(2,n):
c=a+b
a=b
b=c
print(c)
fib(int(input("Enter how many fibonacci numbers needed = ")))
-----------------
Output:
Enter how many Fibonacci numbers needed = 5
0 1 1 2 3
Enter how many Fibonacci numbers needed = 8
0 1 1 2 3 5 8 13
That's it!
Facing any issues comment down we are free to help you.
liked the content don't forget to share with your fellow mates.
No comments:
Post a Comment