Sunday, January 2, 2022

How to print the Fibonacci Series using Python Program

Python Program to print the Fibonacci Series using While Loop
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

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
# ------------------------------------------------------------------------------------------------------------------------ #

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog