S.Y.I.T Sem 3 Python Practical | Python| Python practical 1c| Fibonacci series | python for beginner
Hello reader's ,
In this blog you are going to see that how you can print a Fibonacci series. This blog is for those students who are studying in second year of information technology. This blog is the practical of python subject which is in sem 3.
For performing this practical you must have install python on your laptop or pc. to download python - https://www.python.org/downloads/
python website |
go to the link and download python and you need a IDE which supports python, i am using VS Code, you can use python IDE too.
pycharm logo |
vs code |
After the successful installation open ide and type the code or simply copy the code from given below and run it to see the output.
Don't forget to leave your comment in the comment section below.
CODE -
# Program to display the Fibonacci sequence up to n-th term
nterms = int(input("How many terms? "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
# if there is only one term, return n1
elif nterms == 1:
print("Fibonacci sequence upto",nterms,":")
print(n1)
# generate fibonacci sequence
else:
print("Fibonacci sequence:")
while count < nterms:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Comments
Post a Comment
Thank you for sharing this.
You’re helping us become better. Want to stay in the loop? Subscribe to our YouTube channel.