Python code to find Nth term in Fibonacci series

 

Find the Nth term in the Fibonacci series.

Note: Here Fibonacci series starts with 1 not with 0 {1,1,2,3,5}.

Input Format :
Nth term.
Output Format :
 Return its equivalent Fibonacci number.

Explanation of Input :

The number is ‘5’ so we have to find the “5th” Fibonacci number
So by using the property of the Fibonacci series i.e 

[ 1, 1, 2, 3, 5, 8]
So the “5th” element is “5” hence we get the output.

Sample Input :

6

Sample Output :

8

Solution :

  1. N = int(input())
  2. temp = 1
  3. a = 1
  4. b = 0
  5. c = 0
  6. if N == 1:
  7.     c = 1
  8. else:
  9.     while temp < N:
  10.         c = a + b
  11.         b = a
  12.         a = c
  13.         temp = temp + 1
  14. print(c)
Try it >>
Previous Post Next Post