The Fibonacci number series are 0, 1, 1, 2, 3, 5, 8..etc. The logic of the Fibonacci series is as follows:
f0 = 0
f1 = 1
Now the third Fibonacci number 'f' in the series can be obtained by adding the previous two number in the series.
f = f0 + f1
Now we can print the Fibonacci Series up to the user specified range.
n = int(input('How many Fibonacci''s Series to print: '))
f0 = 0
f1 = 1
f = 0
f1 = 1
f = 0
fiblist = [0, 1]
i = 0
if n == 0:
fiblist = f0
elif n == 1:
fiblist = f0, f1
else:
while i < n-2:
f = f0+f1
fiblist.append(f)
f0, f1 = f1, f
i = i+1
print('The Fibonacci Series between {0} and {1} is: '.format(0, n), fiblist)
# ------------------------------------------------------------------------------------------------------------------------ #Input:
How many Fibonacci's Series to print: 10
Output:
The Fibonacci Series between 0 and 10 is: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
Process finished with exit code 0
#--------------------------------------------------------------Thanks--------------------------------------------------------------#
No comments:
Post a Comment
Hi User, Thank You for visiting My Blog. If you wish, please share your genuine Feedback or comments only related to this Blog Posts. It is my humble request that, please do not post any Spam comments or Advertising kind of comments, which will be Ignored.