Python Program to Find the Fibonacci Series without Using Recursion


When it is required to find the Fibonacci series without using recursion technique, the input is taken from the user, and a ‘while’ loop is used to get the numbers in the sequence.

Example

Below is a demonstration for the same −

 Live Demo

first_num = int(input("Enter the first number of the fibonacci series... "))
second_num = int(input("Enter the second number of the fibonacci series... "))
num_of_terms = int(input("Enter the number of terms... "))
print(first_num,second_num)
print("The numbers in fibonacci series are : ")
while(num_of_terms-2):
   third_num = first_num + second_num
   first_num=second_num
   second_num=third_num
   print(third_num)
   num_of_terms=num_of_terms-1

Output

Enter the first number of the fibonacci series... 2
Enter the second number of the fibonacci series... 8
Enter the number of terms... 8
2 8
The numbers in fibonacci series are :
10
18
28
46
74
120

Explanation

  • The first number and second number inputs are taken from the user.
  • The number of terms is also taken from the user.
  • The first and the second numbers are printed on the console.
  • A while loop begins, and the below takes place −
  • The first and second numbers are added and assigned to a third number.
  • The second number is assigned to the third number.
  • The third number is assigned to the second number.
  • The third number is printed on the console.
  • The number of terms is decremented by 1.

Updated on: 12-Mar-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements