Using recursion we have one way to solve this problem, let us take a look at it.
Method 1: Using multiple conditionals
def get_nth_fibonacci(n): if n<= 0: print("Incorrect input") python-program-f elif n == 1: return 0 elif n == 2: return 1 else: return get_nth_fibonacci(n-1)+get_nth_fibonacci(n-2) n = int(input("Enter the n-th fibonacci number you want: ")) print("The",n,"get_nth_fibonacci number is: ",get_nth_fibonacci(n))
Output:
Enter the n-th fibonacci number you want: 10
The 10 get_nth_fibonacci number is: 34
Explanation:
This is the most basic approach to solving this problem. We used conditional statements to check if the number is 0, if it is zero then we print the “Incorrect input”. In the same way, we defined conditions for 1 and 2 also. Now for the rest of the numbers, we used the function again and computed the value of n-1 and n-2, and finally added them.
Method 2: Using limited conditionals
fibo = [0, 1] def get_nth_fibonacci(n): if n<0: print("Incorrect input") elif n<= len(fibo): return fibo[n-1] else: temp_fib = get_nth_fibonacci(n-1)+get_nth_fibonacci(n-2) fibo.append(temp_fib) return temp_fib print(get_nth_fibonacci(n)) n = int(input("Enter the n-th fibonacci number you want: ")) print("The",n,"get_nth_fibonacci number is: ",get_nth_fibonacci(n))
Output:
Enter the n-th fibonacci number you want: 10
The 10 get_nth_fibonacci number is: 34
Explanation:
The only difference between the previous method and this one is that we did not individually use the conditional for 1 and 2, instead we computed the length of the fibo list and simply returned the value that comes after computing fibo[n-1]. This will only be done if n is smaller than the length of the fibo.
These above are the two methods using which we can solve the given problem.
- Simple Code to compare Speed of Python, Java, and C++?
- Falling Stars Animation on Python.Hub October 2024
- Most Underrated Database Trick | Life-Saving SQL Command
- Python List Methods
- Top 5 Free HTML Resume Templates in 2024 | With Source Code
- How to See Connected Wi-Fi Passwords in Windows?
- 2023 Merry Christmas using Python Turtle
- 23 AI Tools You Won’t Believe are Free
- Python 3.12.1 is Now Available
- Best Deepfake Apps and Websites You Can Try for Fun
- Amazon launched free Prompt Engineering course: Enroll Now
- 10 GitHub Repositories to Master Machine Learning
- Hello World in 35 Programming Languages
- How to Scrape Data From Any Website with Python?
- Become Job Ready With Free Harvard Computer Science course: Enroll Now
- Free Python Certification course from Alison: Good for Resume
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- Udacity Giving Free Python Course: Here is how to Enroll
- Love Babbar’s Income Revealed
- Top 5 Websites to Learn Programming in 2024
- Python Internship for college students and freshers: Apply Here
- Microsoft Giving Free Python Course in 2023: Enroll Now
- Top 5 Free Python Courses on YouTube in 2024
- Complete Python Roadmap for Beginners in 2024
- New secrets to Earn money with Python in 2024
- Connect with HR Directly – Job Hack
- Google offering free Python course: Enroll Today
- What is an AI Tool?
- Google Internship 2024
- TCS Launched Free Certification Course with Industry Recognized Value