Write a Program (WAP) to Print Fibonacci Series in Python using for loop with source code. Further, addition of terms of Fibonacci series. This is a good program for beginners, CBSE KV Class 11 and 12 students practical project file and programmers.
What is Fibonacci Series?
In Fibonacci series numbers go from 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ……
The 3rd term is sum of 1 and 2 and 4th term is sum of 2nd and 3rd term and so on…. In general we can say that the nth term is the sum of (n-1)th and (n-2)th term. Here in starting 0,1 is given as default then this series starts growing as per the definition given.
Write a program WAP to print Fibonacci series up to n terms, also find sum of series.
How our program work – an explanation
n=int(input(“Enter the number of terms in Fibonacci series”)) # n is integer input and asking up to how many terms are
#to be printed
a,b=0,1 #Here a is initialized 0 and b is initialized 1.
s=a+b #sum of first and second term i.e 0,1 then after then in loop other additions
print(a,b,end=” “) #intital 2 means 0 and 1 is printed a=0 b=1
for i in range(n-2): #it is for loop where i (new variable) goes into looping from 1 time to n-2 time . 2 times are printed #from the above line
print(a+b,end=” “) # Now a+b is printed
a,b=b,a+b #now the values will be changed as written this will go on till loop
s=s+b #sum of digits
print(“Sum of”,n,”terms of series =”,s)
Output fibonacci series in python-
The shell window ( above window) asks for input which is how much you want to output.
In the first input we have given 5. Which means it will show 5 value . Now the first 2 digits are 0,1
then the loop starts and runs 3 time to give 1,2,3 . Similarly other inputs are there showing their output in their command.Series and the addition of the numbers is also given in last line .
Next the input is 10 and then 20. check them out they are given above pic.
Source code for the “Write a Program to Print Fibonacci Series in Python “- Click here
For more python programs- Click here