Python Print Fibonacci series: The Fibonacci series is a sequence of numbers where each number (known as a Fibonacci number) is the sum of the two preceding ones, usually starting with 0 and 1. In mathematical terms,
Here’s how the Fibonacci series typically begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, …
Formula
F(n) = F(n-1) + F(n-2) for n > 1
Example –
- F(0) = 0
- F(1) = 1
- F(2) = F(1) + F(0) = 1 + 0 = 1
- F(3) = F(2) + F(1) = 1 + 1 = 2
- F(4) = F(3) + F(2) = 2 + 1 = 3
- F(5) = F(4) + F(3) = 3 + 2 = 5
- And so on…
This sequence continues indefinitely, with each number being the sum of the two preceding numbers.
Python Program to print Fibonacci series
The code begins by defining a Python function named fibonacci(n)
that takes an input integer ‘n’ representing the number of terms to generate in the Fibonacci series. It handles cases where ‘n’ is less than or equal to 0, equal to 1, and greater than 1.
The article walks readers through the logic used in the code, which utilizes two variables, ‘a’ and ‘b,’ to iteratively calculate and print the Fibonacci series. It demonstrates how ‘a’ and ‘b’ are updated in each iteration to produce the sequence accurately.
Algorithm
This algorithm provides a step-by-step description of how the fibonacci
function works without using nested lists. It initializes a
and b
, checks for valid input, prints the Fibonacci series based on the input, and updates the Fibonacci numbers using a loop.
- Start
- Initialize two variables
a
andb
to 0 and 1, respectively. These variables will store the first two Fibonacci numbers. - Input the number of terms to print and store it in a variable
n
. - Check if
n
is less than or equal to 0. If true, print “Please enter a positive integer.” and end the program. - Check if
n
is equal to 1. If true, print “Fibonacci Series up to 1 terms:” followed by the value ofa
(which is 0), and end the program. - If
n
is greater than 1, print “Fibonacci Series up to n terms:” followed by the values ofa
andb
separated by a comma. - Initialize a loop variable
i
to 2. - Enter a loop that continues until
i
reachesn
. a. Calculate the next Fibonacci numberc
by addinga
andb
. b. Printc
followed by a comma. c. Updatea
andb
by assigningb
toa
andc
tob
for the next iteration. d. Incrementi
by 1. - End the loop.
- End
Source Code
his code defines a function to print the Fibonacci series up to a specified number of terms and then takes user input to determine how many terms of the Fibonacci series should be printed. It handles cases where n
is less than or equal to 0, equal to 1, or greater than 1.
# Function to print Fibonacci series up to n terms def fibonacci(n): # Initialize the first two Fibonacci numbers a, b = 0, 1 # Check if the input is valid if n <= 0: print("Please enter a positive integer.") elif n == 1: print("Fibonacci Series up to", n, "terms:") print(a) else: print("Fibonacci Series up to", n, "terms:") print(a, end=", ") print(b, end=", ") # Calculate the remaining terms for i in range(2, n): c = a + b print(c, end=", ") # Update a and b for the next iteration a, b = b, c # Input the number of terms you want to print n = int(input("Enter the number of Fibonacci terms to print: ")) # Call the fibonacci function fibonacci(n)
Steps to Write Code
Step-1: Define a function named fibonacci
that takes a single parameter n
.
def fibonacci(n):
Step-2: Inside the function, initialize two variables, a
and b
, to 0 and 1, respectively.
a, b = 0, 1
Step-3: Check if the value of n
is less than or equal to 0. If it is, print a message asking the user to input a positive integer.
if n <= 0: print("Please enter a positive integer.")
Step-4: If n
is equal to 1, then print the Fibonacci series up to the first term, which is 0.
elif n == 1: print("Fibonacci Series up to", n, "terms:") print(a)
Step-5: If n
is greater than 1, print the Fibonacci series up to n
terms. Initially, print the first two terms (a
and b
) with a comma and space as separators.
else: print("Fibonacci Series up to", n, "terms:") print(a, end=", ") print(b, end=", ")
Step-6: Enter a loop that iterates from i
equals 2 to n - 1
. In each iteration, calculate the next Fibonacci number c
by adding a
and b
, and then print c
with a comma and space as separators.
for i in range(2, n): c = a + b print(c, end=", ")
Step-7: After printing each c
, update the values of a
and b
for the next iteration to keep track of the two most recent Fibonacci numbers.
a, b = b, c
Step-8: Prompt the user to input the number of Fibonacci terms they want to print using the input
function.
n = int(input("Enter the number of Fibonacci terms to print: "))
Step-9: Call the fibonacci(n)
function with the user’s input as the argument to generate and print the Fibonacci series.
fibonacci(n)
Output
Let’s say the user enters n = 8
when prompted.
Here’s the expected output
Enter the number of Fibonacci terms to print: 8 Fibonacci Series up to 8 terms: 0, 1, 1, 2, 3, 5, 8, 13,
In this example, the code generates and prints the Fibonacci series up to 8 terms, as specified. The series starts with 0 and 1 as the first two terms and then continues with the next terms calculated using the Fibonacci sequence. Each term is separated by a comma and a space.