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.
- Unveiling the Future of AI Detector
- CodeWithHarry Earns 20 Lakhs per month from YouTube?
- Cleaning Service Booking System in Python Tkinter
- Farmers Ecommerce App using Python Tkinter
- Guidelines for Project Collaboration Process
- The system of the binary conversion
- What is web development for beginners?
- Guide to Proxy Servers: How They Work and Why You Need Them?
- Python | Check Armstrong Number using for loop
- Python | Factorial of a number using for loop
- Link in bio
- Microsoft Giving Free Machine Learning Course: Enroll Now
- Accenture Giving Free Developer Certificate in 2023
- Python | Asking the user for input until they give a valid response
- Python | How to iterate through two lists in parallel?
- Amazon Summer Internship 2023
- Python | How to sort a dictionary by value?
- Amazon Giving Free Machine Learning Course with Certificate: Enroll Now
- Google Summer Internship 2023
- Python | How to split a list into equally-sized chunks?
- 5 Secret ChatGPT skills to make money
- Python | Remove items from a list while iterating
- Free Google Certification Courses
- 5 AI tools for coders better than ChatGPT
- Python | How to get dictionary keys as a list
- New secrets to Earn money with Python in 2023
- Flower classification using CNN
- How to represent Enum in Python?
- 5 methods | Flatten a list of lists | Convert nested list into a single list in Python
- What does if __name__ == __main__ do in Python?