Python Fibonacci Series program

How to Write Python Fibonacci Series program using While Loop, For Loop, and Recursion?. As per Mathematics, Fibonacci numbers or series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …

Python Fibonacci Series program using While Loop

This program allows the user to enter any positive integer. Next, this program displays the Fibonacci series numbers from 0 to user-specified numbers using While Loop.

Number = int(input("\nPlease Enter the Range : "))

# Initializing First and Second Values
i = 0
First_Value = 0
Second_Value = 1
           
# Find & Displaying
while(i < Number):
    if(i <= 1):
        Next = i
    else:
        Next = First_Value + Second_Value
        First_Value = Second_Value
        Second_Value = Next
    print(Next)
    i = i + 1
Please Enter the Range : 4
0
1
1
2

This program allows the user to enter any positive integer, which is then assigned to a variable Number. Next, We declared three integer variables i, First_Value, and Second_Value, and assigned values.

The below While Loop makes sure that the loop starts from 0 and it is less than the user-given number. Within the While loop of the Python Fibonacci series program, we used the If statement.

  • If i value is less than or equal to 1, then Next = i
  • If i value is greater than 1, perform calculations inside the Else block.

Let us see the working principle of this while loop in this example in iteration wise. In this example, User Entered value: Number = 4 and i = 0, First_Value = 0, Second_Value = 1

While Loop First Iteration

  • While (0 < 4) is TRUE. So, program starts executing statements inside the while.
  • Within the while loop, we have the If statement and the condition if (0 <= 1) is TRUE. So, Next = 0, and the compiler exit from the if statement block.
  • Print statement print(Next) print the value 0.
  • Lastly, i incremented to 1.

while loop Second Iteration

  • While (1 < 4) is TRUE. 
  • Within a while, we have Python If statement, and the condition if (1 <= 1) is TRUE. So, Next = 1 and the compiler exits from the if statement block.
  • Print statement print(Next) print the value 1.
  • i incremented to 1.

Third Iteration: While (2 < 4) is TRUE in this example. The condition if (2 <= 1) is FALSE, so statements inside the else block start executing.

Next = First_Value + Second_Value
Next = 0 + 1 = 1
First_Value = Second_Value = 1
Second_Value = Next = 1

Next, Print statement print(Next) in the program print the value 1. Lastly, i incremented to 1

Fourth Iteration: while (3 < 4) is TRUE. So, the program starts executing statements inside the while.

The condition if (3 <= 1) is FALSE
Next = 1 + 1 = 2
First_Value = Second_Value = 1
Second_Value = Next = 2

Next, Print statement print(Next) print the value 2. Lastly, i incremented to 1

Fifth Iteration: While (4 < 4) is FALSE, it exits from the while loop. Please refer to For Loop.

Our final output of the Next values are: 0 1 1 2

Python Fibonacci Series program using For Loop

This program displays the Fibonacci series of numbers from 0 to user-specified value using For Loop.

# It will start at 0 and travel upto below value
Number = int(input("\nPlease Enter the Range : "))

# Initializing First and Second Values 
First = 0
Second = 1
           
# Find & Displaying
for Num in range(0, Number):
    if(Num <= 1):
        Next = Num
    else:
        Next = First + Second
        First = Second
        econd = Next
    print(Next)
Please Enter the Range : 10
0
1
1
2
3
5
8
13
21
34

Python Fibonacci Series program using Recursion

This program displays the Fibonacci series of numbers from 0 to user-given value using the Recursion concept.

# Recursive Function Beginning

def fibFind(num):
    if(num == 0):
        return 0
    elif(num == 1):
        return 1
    else:
        return (fibFind(num - 2)+ fibFind(num - 1))

# End of the Function

# It will start at 0 and travel upto below value
num = int(input("\nPlease Enter the Range Number: "))

# Find & Displaying Them
for Num in range(0, num):
    print(fibFind(Num))
Python Fibonacci Series Program using Recursion

In this Fibonacci Series program, using the recursion example, we defined a function. The following function accept integer values as parameter value and return value.

def fibFind(num):

Let’s see the Elif statement inside the above-specified functions

  • if (num == 0) checks whether the given number is 0 or not. If it is TRUE, the function returns the value Zero.
  • if(num == 1) check whether the given number is 1 or not. If it is TRUE, the function returns the value One.
  • And, if the number is greater than 1, the statements inside the else block are executed.

Within the Else block, we called the function recursively to display the result.

return (fibFind(num-2)+ fibFind(num-1))

For the demonstration using recursion, Number= 2

fibFind(num-2)+ fibFind(num-1)

fibFind(2 – 2)+ fibFind(2 – 1)

It means, (fibFind(0)+ fibFind(1))

return (0 + 1) = return 1

Comments are closed.