Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Python Program for n-th Fibonacci number - Python Program for n-th Fibonacci number

Python Program for n-th Fibonacci number

Posted on December 8, 2024December 21, 2024 By Rishabh saini No Comments on Python Program for n-th Fibonacci number

Python Program for n-th Fibonacci number

Python Program for n-th Fibonacci number

In this tutorial, we will explore how to compute the nth Fibonacci number using Python. Each number in the series of Fibonacci numbers is equal to the sum of the two numbers that came before it. Starting with 0 and 1, the sequence begins.

For example,

  1. 0 and 1 are the first two words.
  2. The formula for the third term is 0+1=10 + 1 = 1.
  3. 1+1=21 + 1 = 2 is the fourth phrase, and so forth.

The recurrence relation is used to mathematically define this sequence: F(n)=F(n−1)+F(n−2)+ F(n-1) + F(n-2) = F(n)

There are multiple ways to find the nth Fibonacci number in Python, including:

  1. Using Recursion
  2. Using Dynamic Programming
  3. Using Dynamic Programming with Space Optimization
  4. Using Arrays

In this article, we’ll focus on the most fundamental approaches and understand them with examples.

Download New Real Time Projects :-Click here


1. Finding nth Fibonacci Number Using Recursion

Recursion refers to a function calling itself until a specific condition is met. It’s a straightforward yet powerful approach for solving problems like finding Fibonacci numbers.

Example:

# Function to calculate the nth Fibonacci number
def Fibonacci_Series(n):
    if n < 0:
        print("Invalid input! Fibonacci numbers are only defined for non-negative integers.")
    elif n == 0:
        return 0
    elif n == 1:
        return 1
    else:
        return Fibonacci_Series(n - 1) + Fibonacci_Series(n - 2)

# Testing the function
print("12th Fibonacci Number:", Fibonacci_Series(12))

Output:

12th Fibonacci Number: 144

Explanation:

  • The function Fibonacci_Series(n) recursively calculates Fibonacci numbers.
  • Base cases: It gives 0 if n=0n = 0 and 1 if n=1n = 1.
  • Until it reaches the base cases, the function calculates F(n−1)+F(n−2)F(n-1) + F(n-2) for n>1n > 1.

While recursion is intuitive, it can be inefficient for larger values of nn due to repeated calculations.


2. Finding nth Fibonacci Number Using Dynamic Programming

Dynamic Programming avoids repeated calculations by storing intermediate results. This makes the approach efficient.

Example:

# Dynamic programming function to determine the nth Fibonacci number
def Fibonacci_series(n):
    fib_array = [0, 1]  # Initializing the first two Fibonacci numbers

    for i in range(2, n + 1):
        fib_array.append(fib_array[i - 1] + fib_array[i - 2])

    return fib_array[n]

# Testing the function
print("12th Fibonacci Number:", Fibonacci_series(12))

Output:

12th Fibonacci Number: 144

Explanation:

  • A list fib_array stores Fibonacci numbers as they are computed.
  • The for-loop iterates from index 2 to nn, adding the two preceding values and appending the result to the list.
  • Finally, the nth Fibonacci number is returned.


3. Dynamic Programming with Space Optimization

In this method, we further optimize space usage by only keeping the last two computed Fibonacci numbers instead of storing the entire sequence.

Example:

# Function to calculate the nth Fibonacci number with space optimization
def Fibonacci_series(n):
    if n < 0:
        print("Invalid input!")
    elif n == 0:
        return 0
    elif n == 1:
        return 1

    # Initialize the first two numbers
    prev1, prev2 = 0, 1

    for _ in range(2, n + 1):
        current = prev1 + prev2
        prev1, prev2 = prev2, current  # Update the last two numbers

    return prev2

# Testing the function
print("12th Fibonacci Number:", Fibonacci_series(12))

Output:

12th Fibonacci Number: 144

Explanation:

  • We only use two variables (prev1 and prev2) to track the last two Fibonacci numbers.
  • The for-loop iteratively calculates the next number while updating the variables.
  • This approach is both time and space-efficient.


4. Finding nth Fibonacci Number Using Arrays

In this method, an array is explicitly created to store all Fibonacci numbers up to the nth term.

Example:

# Function to calculate the nth Fibonacci number using an array
def Fibonacci_series(n):
    # Create an array to store Fibonacci numbers
    fib_array = [0] * (n + 1)
    fib_array[1] = 1

    for i in range(2, n + 1):
        fib_array[i] = fib_array[i - 1] + fib_array[i - 2]

    return fib_array[n]

# Testing the function
print("12th Fibonacci Number:", Fibonacci_series(12))

Output:

12th Fibonacci Number: 144

Explanation:

  • The array fib_array is initialized to store n+1n + 1 elements.
  • Starting with 0 and 1, the loop fills in the rest of the array by summing the preceding two elements.
  • Finally, the nth Fibonacci number is returned.

In this tutorial, we explored various methods to calculate the nth Fibonacci number using Python. Each method has its own advantages:

  • Recursion: Simple but inefficient for large inputs.
  • Dynamic Programming: More efficient by avoiding redundant calculations.
  • Space Optimization: Reduces memory usage.
  • Arrays: Useful for scenarios where the entire sequence is needed.

Choose the approach that best suits your requirements!


  1. PHP PROJECT:- CLICK HERE
  2. INTERVIEW QUESTION:-CLICK HERE
  3. Complete Advance AI topics:- CLICK HERE
  4. Complete Python Course with Advance topics:- CLICK HERE


fibonacci series program in python using for loop
fibonacci series in python
python program for n-th fibonacci number
Python Program for n-th Fibonacci number
python program for how to check if a given number is fibonacci number
fibonacci series in python class 11
python program for n-th fibonacci number with code
write a program to print fibonacci series in python using while loop
fibonacci series in python w3schools
python program for n-th fibonacci number with code
python program for n-th fibonacci number code
python program for n-th fibonacci number
fibonacci series using recursion in python
nth fibonacci number formula
Python Program for n-th Fibonacci number
python program for n-th fibonacci number

Post Views: 737
Python Tags:fibonacci, fibonacci number, fibonacci number in python, fibonacci series, fibonacci series in python, fibonacci series program in python, nth fibonacci number in python, Python, python fibonacci, python for beginners, python program for fibonacci series, python program for generating fibonacci series, python program for n th fibonacci number, python program for nth fibonacci number, python programming, write a python program for nth fibonacci number

Post navigation

Previous Post: Mobile Shop Management System in PHP With Source Code
Next Post: How to Use AI in Marketing

More Related Articles

Python Tkinter Label Python Tkinter Label: A Complete Guide Python
Python Course Roadmap: From Basics to Advance (Day-45 Road Map) Python
Python Multiprocessing: A Complete Guide - Screenshot 2024-11-29 200152 Python Multiprocessing: A Complete Guide Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may also like

  1. Python Decorators: A Comprehensive Guide
  2. How to Calculate Distance between Two Points using GEOPY
  3. Finding the Second Largest Number in Python
  4. Python Constructor: A Guide to Initializing Objects in Python
  5. Weather Information App
  6. How to Install Django: Step-by-Step Guide

Most Viewed Posts

  1. Top Large Language Models in 2025
  2. Online Shopping System using PHP, MySQL with Free Source Code
  3. login form in php and mysql , Step-by-Step with Free Source Code
  4. Flipkart Clone using PHP And MYSQL Free Source Code
  5. News Portal Project in PHP and MySql Free Source Code
  6. User Login & Registration System Using PHP and MySQL Free Code
  7. Top 10 Final Year Project Ideas in Python
  8. Online Bike Rental Management System Using PHP and MySQL
  9. E learning Website in php with Free source code
  10. E-Commerce Website Project in Java Servlets (JSP)
  • AI
  • ASP.NET
  • Blockchain
  • ChatCPT
  • code Snippets
  • Collage Projects
  • Data Science Project
  • Data Science Tutorial
  • DBMS Tutorial
  • Deep Learning Tutorial
  • Final Year Projects
  • Free Projects
  • How to
  • html
  • Interview Question
  • Java Notes
  • Java Project
  • Java Script Notes
  • JAVASCRIPT
  • Javascript Project
  • JSP JAVA(J2EE)
  • Machine Learning Project
  • Machine Learning Tutorial
  • MySQL Tutorial
  • Node.js Tutorial
  • PHP Project
  • Portfolio
  • Python
  • Python Interview Question
  • Python Projects
  • PythonFreeProject
  • React Free Project
  • React Projects
  • Spring boot
  • SQL Tutorial
  • TOP 10
  • Uncategorized
  • Online Examination System in PHP with Source Code
  • AI Chatbot for College and Hospital
  • Job Portal Web Application in PHP MySQL
  • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
  • Online Job Portal System in JSP Servlet MySQL

Most Viewed Posts

  • Top Large Language Models in 2025 (8,614)
  • Online Shopping System using PHP, MySQL with Free Source Code (5,216)
  • login form in php and mysql , Step-by-Step with Free Source Code (4,870)

Copyright © 2026 UpdateGadh.

Powered by PressBook Green WordPress theme