# Fibonacci series in python using function

In this post, I am explaining how to print the fibonacci series in python using function. Fibonacci series is, in which each number is the addition of two previous numbers for example,

0 1 1 2 3 5 8 13 21 . . .

So, here we can see that 0+1 is 1 and 1+1 is 2 and 2+3 is 5 respectively, we can print Fibonacci series in that manner.

we are going to make a programme in which we are using the function in python to print the Fibonacci series. also, we are using the for loop in the function to print the series. so let's see the program now,

def fib (n):

a=0

b=1

print(a)

print(b)

for i in range(2,n):

c=a+b

a=b

b=c

print(c)

fib(5)

- First, by using "def" we create a function called fib() in which we have given a parameter n.
- This series is starting from 0 and 1 so that we assign these initial values to the variable
**a**and**b**respectively. - Now we are printing the values of
**a**and**b,**which is 0 and 1. - We already printed the first two numbers of our series so that now by using for loop we are taking counter variable
**i**. - By using the range function we are going from 2 to n,
**n**is the number we take from user which shows how many numbers he wants to print. - c=a+b that is now 0+1 which is 1 and, 1 is stored in c now.
- a=b that shows the value of 1is in a now
- and b=c the value of c i.e is 1 now.
- we print 1 now by printing c.
- And for loop will go up to n.
- We are calling the function now, and giving n=5.
- The output of the program is given below.

__Output__

0

1

1

2

3

thank for reading my post, if you have any query please mention that in comment section,

## No comments:

## Post a Comment