Finding the N'th number in the Fibonacci sequence

What is a Fibonacci Sequence
The Fibonacci sequence starts with the numbers 0 followed by 1. The subsequent number is the addition of the previous two numbers. Thus the Fibonacci sequence looks like below
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on.

Note : Generating the n’th number in Fibonacci sequence using recursion is very inefficient as we come across numerous overlapping sub-problems. A better idea is to use an iterative or dynamic programming approach.


Algorithm : Finding the n’th Fibonacci number
FibonacciNumber ( n )
1.   If n is 0 or 1
2.       return n
3.   Else return the sum of the previous two Fibonacci numbers.
4.       return FibonacciNumber (n-1) + FibonacciNumber (n-2)


Java

Java Program for finding the N'th Fibonacci number using recursion.


C++

C++ Program for finding the N'th Fibonacci number using recursion.


Python Program for finding the n’th Fibonacci number using recursion.

def Fibonacci (n) :

    if n <= 1  :
       return n 

    return Fibonacci (n-1) + Fibonacci (n-2)
    
def main() :
    n = int(input("Finding n'th Fibonacci(n) number. Enter n : ")) 
    if n <= 1 : 
        print(n, end = ' ')
    else :
        print(int(Fibonacci(n)))

if __name__ == "__main__" :
    main()

Output of finding the n’th Fibonacci number implemented in Python3

Finding n'th Fibonacci(n) number. Enter n : 10
55
Finding n'th Fibonacci(n) number. Enter n : 2
1
Finding n'th Fibonacci(n) number. Enter n : 11
89
Finding n'th Fibonacci(n) number. Enter n : 12
144

Copyright (c) 2019-2020, Algotree.org.
All rights reserved.