Nth Fibonacci number
Level EASY
Nth term of fibonacci series F(n) is calculated using following formula -
Provided N you have to find out the Nth Fibonacci Number.
Input Format :
Output Format :
Constraints:
Sample Input 1:
Sample Output 2:
Sample Input 1:
Sample Output 2:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Read input as specified in the question. | |
## Print output as specified in the question. | |
FibArray = [1, 1] | |
def fibonacci(n): | |
if n < 0: | |
print("Incorrect input") | |
elif n <= len(FibArray): | |
return FibArray[n - 1] | |
else: | |
temp_fib = fibonacci(n - 1) + fibonacci(n - 2) | |
FibArray.append(temp_fib) | |
return temp_fib | |
# Driver Program | |
num=int(input()) | |
print(fibonacci(num)) |
Comments
Post a Comment
Please give us your valuable feedback