Skip to content
Home » Find nth Fibonacci Number in Python

Find nth Fibonacci Number in Python

Learn about Find nth Fibonacci Number in Python in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Find nth Fibonacci Number in Python

# Python program to find n-th fibonacci number

# input
num = int(input('Enter number of terms: '))

a, b = 0, 1
    
# check if the number of terms is valid
if num <= 0:
    print('Please enter a positive integer.')

elif num == 1:
    print('The Fibonacci term is = ', end='')
    print(a)

else:
    print('The Fibonacci term is = ', end='')
    for i in range (2, num):
        c = a + b
        a = b
        b = c
    print(b)

Output:

Enter number of terms: 4
The Fibonacci term is = 2

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Code : Fibonacci Series in Python using For loop