In this Python program, we will learn how to print the Fibonacci series.
In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,
F0=0 and F1=1
And
Fn=Fn-1 + Fn-2
Examples of Fibonacci Series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 and so on.
Here is the source code to print the Fibonacci series.
# Python Program to Print the Fibonacci Series
# Fibonacci series will start at 0 and travel upto below number
num = int(input("Enter the Range Number: "))
# first two terms
n1, n2 = 0, 1
count = 0
# check if the number of terms is valid
if num <= 0:
print("Please enter a positive integer")
elif num == 1:
print("Fibonacci sequence upto",num,":")
print(n1)
else:
print("Fibonacci sequence:")
while count < num:
print(n1)
nth = n1 + n2
# update values
n1 = n2
n2 = nth
count += 1
Enter the Range Number: 10
Fibonacci sequence:
0
1
1
2
3
5
8
13
21
34
Comments