Python

Fibonacci Series in Python using While Loop

In this tutorial, we are going to see how to display the Fibonacci sequence using while loop.

Fibonacci sequence is a sequence of integers of 0, 1, 2, 3, 5, 8…

The first two terms are 0 and 1. All the other terms are obtained by adding the two previous terms. This means that the nth term is the sum of the (n-1)th and (n-2)th term.
 
 

Fibonacci Series in Python using While Loop
nterms = int(input("Enter a number: "))
 
n1 = 0
n2 = 1
 
print("\n The fibonacci sequence is :")
print(n1, ",", n2, end=", ")
 
for i in range(2, nterms):
	next = n1 + n2
	print(next, end=", ")
 
	n1 = n2
	n2 = next

This example will print the Fibonacci sequence of 10.

Enter a number: 10

The fibonacci sequence is :
0 , 1, 1, 2, 3, 5, 8, 13, 21, 34,
🚀 Boost your productivity with the best AI toolsTry them

 
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *