Python Program to Find the Fibonacci Series Without using Recursion

This is a Python Program to find the fibonacci series without using recursion.

Problem Description

The program takes the first two numbers of the series along with the number of terms needed and prints the fibonacci series.

Problem Solution

1. Take the first two numbers of the series and the number of terms to be printed from the user.
2. Print the first two numbers.
3. Use a while loop to find the sum of the first two numbers and then proceed the fibonacci series.
4. Print the fibonacci series till n-2 is greater than 0.
5. Exit.

Program/Source Code

Here is source code of the Python Program to find the fibonacci series without using recursion. The program output is also shown below.

a=int(input("Enter the first number of the series "))
b=int(input("Enter the second number of the series "))
n=int(input("Enter the number of terms needed "))
print(a,b,end=" ")
while(n-2):
    c=a+b
    a=b
    b=c
    print(c,end=" ")
    n=n-1
Program Explanation

1. User must enter the first two numbers of the series and the number of terms to be printed.
2. The first two terms are printed outside the while loop.
3. A while loop is used to find the sum of the first two terms and proceed the series by interchanging the variables.
4. The value of n is decremented.
5. The fibonacci series is printed till n-2 is greater than 0.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter the first number of the series 0
Enter the second number of the series 1
Enter the number of terms needed 4
0 1 1 2 
 
Case 2:
Enter the first number of the series 2
Enter the second number of the series 4
Enter the number of terms needed 5
2 4 6 10 16

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Note: Join free Sanfoundry classes at Telegram or Youtube

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.