Questions

How do you do the Fibonacci Sequence in Python?

Fibonacci Sequence in python can be found in different ways, as suitable for the programmer. One of the most straightforward ways, that is, through the loop, is provided in the source code mentioned above. Another method is recursion. The source code using recursion is provided below.

                    

def Fibonacci (n):

              if n < 0:

                             print (“Invalid input. Series cannot be calculated.”)

              elif n == 0:

                             return 0

              elif n == 1 or n == 2:

                             return 1

              else:

                             return Fibonacci(n-1) + Fibonacci(n-2)

print (Fibonacci (10))

Related Questions

Related Topics

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.