Class 11 Exam  >  Class 11 Questions  >  Write a Python program to generate Fibonacci ... Start Learning for Free
Write a Python program to generate Fibonacci series?
Most Upvoted Answer
Write a Python program to generate Fibonacci series?
Generating Fibonacci series in Python


Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1.


Approach



  1. Take input from the user for the number of terms in the series.

  2. Initialize two variables a and b with the first two terms of the series, i.e., 0 and 1.

  3. Then, using a while loop, generate the series and print it.



Code


```python
# taking input from user
nterms = int(input("How many terms? "))

# initialization
a, b = 0, 1
count = 0

# check if the number of terms is valid
if nterms <=>
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(a)
else:
print("Fibonacci sequence:")
while count < />
print(a)
nth = a + b
# update values
a = b
b = nth
count += 1
```

Explanation


In the above code, we first take input from the user for the number of terms in the series. Then we initialize two variables a and b with the first two terms of the series, i.e., 0 and 1. We also initialize a variable count to keep track of the number of terms generated so far.


Next, we check if the number of terms is valid or not. If nterms is less than or equal to 0, we print an error message and exit the program. If nterms is 1, we print the first term of the series and exit the program.


If nterms is greater than 1, then we print the first two terms of the series, and using a while loop, we generate the remaining terms of the series and print them. In each iteration of the while loop, we first print the current value of a, which is the nth term of the series. Then we calculate the next term of the series, which is the sum of the current term a and the previous term b. Finally, we update the values of a and b to generate the next term of the series.


Once all the terms in the series have been generated and printed, the program terminates.
Community Answer
Write a Python program to generate Fibonacci series?
N=int (input ("Enter how many numbers you want in this series:"))
first=0
second=1
for i in range (n):
print(first)
temp=first
first=second
second =temp+second
Attention Class 11 Students!
To make sure you are not studying endlessly, EduRev has designed Class 11 study material, with Structured Courses, Videos, & Test Series. Plus get personalized analysis, doubt solving and improvement plans to achieve a great score in Class 11.
Explore Courses for Class 11 exam

Top Courses for Class 11

Write a Python program to generate Fibonacci series?
Question Description
Write a Python program to generate Fibonacci series? for Class 11 2024 is part of Class 11 preparation. The Question and answers have been prepared according to the Class 11 exam syllabus. Information about Write a Python program to generate Fibonacci series? covers all topics & solutions for Class 11 2024 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Write a Python program to generate Fibonacci series?.
Solutions for Write a Python program to generate Fibonacci series? in English & in Hindi are available as part of our courses for Class 11. Download more important topics, notes, lectures and mock test series for Class 11 Exam by signing up for free.
Here you can find the meaning of Write a Python program to generate Fibonacci series? defined & explained in the simplest way possible. Besides giving the explanation of Write a Python program to generate Fibonacci series?, a detailed solution for Write a Python program to generate Fibonacci series? has been provided alongside types of Write a Python program to generate Fibonacci series? theory, EduRev gives you an ample number of questions to practice Write a Python program to generate Fibonacci series? tests, examples and also practice Class 11 tests.
Explore Courses for Class 11 exam

Top Courses for Class 11

Explore Courses
Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev