Write a program in Python to print the Fibonacci sequence upto n terms.
Answers
Answer:
Python Program to Print Fibonacci Series
Here you will get python program to print fibonacci series using for loop.
A series in which next term is obtained by adding previous two terms is called fibonacci series. For example 0, 1, 1, 2, 3, 5, 8 . . . .
Python Program to Print Fibonacci Series
num = int(input("enter number of digits you want in series (minimum 2): "))
first = 0
second = 1
print("\nfibonacci series is:")
print(first, ",", second, end=", ")
for i in range(2, num):
next = first + second
print(next, end=", ")
first = second
second = next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
num = int(input("enter number of digits you want in series (minimum 2): "))
first = 0
second = 1
print("\nfibonacci series is:")
print(first, ",", second, end=", ")
for i in range(2, num):
next = first + second
print(next, end=", ")
first = second
second = next
Output
enter number of digits you want in series (minimum 2): 6
fibonacci series is:
0 , 1, 1, 2, 3, 5,
Answer:
Python program to print the Fibonacci sequence up to n terms.
Explanation:
# Program to display the Fibonacci sequence up to nth term where n is provided by the user
# change this value for a different result
terms = 10
# uncomment to take input from the user
#nterms = int(input("How many terms? "))
# first two terms
n1 = 0
n2 = 1
count = 0
# check if the number of terms is valid
if nterms <= 0:
print("Please enter a positive integer")
elif nterms == 1:
print("Fibonacci sequence upto", nterms,":")
print(n1)
else:
else:
print("Fibonacci sequence upto", nterms,":")
while count < nterms:
print(n1,end=' , ')
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1